Performance: Why real-time matters
Real-time affects how quickly users see updates and interact with the app, impacting responsiveness and user experience.
const http = require('http'); const express = require('express'); const { Server } = require('socket.io'); const app = express(); const server = http.createServer(app); const io = new Server(server); io.on('connection', (socket) => { // Push updates instantly socket.emit('update', { data: 'new data' }); }); server.listen(3000);
app.get('/updates', (req, res) => { // Endpoint for client-side polling every 5 seconds res.json({ data: 'new data' }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Polling HTTP requests every 5 seconds | Multiple repeated DOM updates | Multiple reflows per update | High paint cost due to frequent updates | [X] Bad |
| WebSocket push updates | Single DOM update per event | Minimal reflows triggered | Lower paint cost with targeted updates | [OK] Good |
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', (socket) => {
console.log('User connected');
socket.on('message', (msg) => {
console.log('Message:', msg);
});
});
server.listen(3000);const express = require('express');
const app = express();
const io = require('socket.io')(app);
io.on('connection', (socket) => {
console.log('Client connected');
});
app.listen(3000);Pass the test and this pattern moves unknown