Performance: Creating a basic HTTP server
MEDIUM IMPACT
This concept affects the initial server response time and how quickly the browser receives the first byte of content.
import http from 'http'; const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/html'}); function writeLines(i) { let ok = true; while (i < 10000 && ok) { ok = res.write(`<p>Line ${i}</p>`); i++; } if (i < 10000) { res.once('drain', () => writeLines(i)); } else { res.end(); } } writeLines(0); }); server.listen(3000);
import http from 'http'; const server = http.createServer((req, res) => { let html = ''; for (let i = 0; i < 10000; i++) { html += '<p>Line ' + i + '</p>'; } res.writeHead(200, {'Content-Type': 'text/html'}); res.end(html); }); server.listen(3000);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous large string build before response | N/A (server-side) | N/A | N/A | [X] Bad |
| Streaming response in chunks | N/A (server-side) | N/A | N/A | [OK] Good |