This comes from some weird memory behaviour in a Discord bot, so I stripped it down to just look at high throughput websockets.
This is the code for the server:
import { randomBytes } from 'crypto'
Bun.serve({
fetch (req, server) {
server.upgrade(req)
},
websocket: {
open (ws) {
setInterval(() => {
// changed 200 to 10000 the second test
const data = { d: randomBytes(200).toString('hex') }
ws.send(JSON.stringify(data))
}, 200)
}
}
})
And my client code (I just logged the rss every minute to copy into sheets):
const ws = new WebSocket(`ws://localhost:3000`)
ws.addEventListener('open', () => console.log('open'))
ws.addEventListener('close', ({ code }) => console.log('ws close, code', code))
// listener 1
ws.addEventListener('message', event => {})
// listener 2
ws.addEventListener('message', event => JSON.parse(event.data.toString()))
setInterval(() => {
const { rss } = process.memoryUsage()
console.log(Math.round(rss / 100_000) / 10)
}, 60_000)
I tested with neither message listener, number 1, and number 2 all simultaneously, which showed an interesting result, so I also tested with a way higher amount of data from the server. It looks like it's mainly JSON.parse causing issues, because having that listener enabled grew memory super fast, but an empty listener also seemed to cause a small problem too - so there might be multiple things going on here.

