Performance: Emitting and receiving messages
MEDIUM IMPACT
This concept affects server response time and network latency impacting how fast messages are sent and received between client and server.
app.get('/send', (req, res) => { // Using setImmediate to batch emits asynchronously let i = 0; function emitBatch() { for(let count = 0; count < 100; count++, i++) { io.emit('message', { text: `Message ${i}` }); if(i >= 10000) return res.send('Messages sent'); } setImmediate(emitBatch); } emitBatch(); });
app.get('/send', (req, res) => { // Emitting message inside a heavy synchronous loop for(let i = 0; i < 10000; i++) { io.emit('message', { text: `Message ${i}` }); } res.send('Messages sent'); });
| Pattern | Event Loop Impact | Network Load | Client Responsiveness | Verdict |
|---|---|---|---|---|
| Synchronous heavy emits | Blocks event loop | High burst load | Delayed input response | [X] Bad |
| Asynchronous batched emits | Non-blocking event loop | Evenly distributed load | Fast input response | [OK] Good |