Performance: Broadcasting to connected clients
MEDIUM IMPACT
This concept affects the responsiveness and scalability of real-time communication by how efficiently messages are sent to multiple clients.
const messageBuffer = Buffer.from(message); clients.forEach(client => { if (client.readyState === WebSocket.OPEN) { client.send(messageBuffer); } });
for (const client of clients) { if (client.readyState === WebSocket.OPEN) { client.send(message); } }
| Pattern | CPU Usage | Event Loop Blocking | Memory Usage | Verdict |
|---|---|---|---|---|
| Sending messages one by one with string conversion | High | High (blocks event loop) | High (many string copies) | [X] Bad |
| Pre-buffering message and sending asynchronously | Low | Low (non-blocking) | Low (single buffer reuse) | [OK] Good |