Prune closed connections before broadcasting

Node · Node · intermediate · modification

Simplifies pruneClosed: instead of allocating a filtered copy and rebuilding the array, it now walks the connections in place and splices out any socket that isn't OPEN. Same result, one less array allocation per broadcast.

connections is the live subscriber list of an in-process WebSocket hub; pruneClosed is called before each fan-out so we never write to a dead peer. Sockets close in bursts (a dropped client often takes its neighbours in the same tick), so adjacent closed entries are common.

Requirements

Files touched

--- src/realtime/connections.js
 // broadcast to a dead peer. Called before each fan-out.
 function pruneClosed(connections) {
-  const alive = connections.filter((conn) => conn.socket.readyState === OPEN);
-  connections.length = 0;
-  connections.push(...alive);
+  connections.forEach((conn, i) => {
+    if (conn.socket.readyState !== OPEN) {
+      connections.splice(i, 1);
+    }
+  });
 }
 

Review this PR

Node practice