Performance: WebSocket protocol concept
MEDIUM IMPACT
WebSocket affects the speed and responsiveness of real-time data exchange between client and server.
import { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); wss.on('connection', ws => { // Persistent connection allows sending updates anytime setInterval(() => { ws.send('Update data'); }, 1000); });
const http = require('http'); const server = http.createServer((req, res) => { // On each request, server sends data and closes connection res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Update data'); }); server.listen(8080);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| HTTP polling for updates | Multiple new DOM updates per request | Triggers reflow per update | High paint cost due to frequent changes | [X] Bad |
| WebSocket persistent connection | DOM updates only on new data | Minimal reflows triggered | Lower paint cost with batched updates | [OK] Good |