0
0
Node.jsframework~8 mins

Why building HTTP servers matters in Node.js - Performance Evidence

Choose your learning style9 modes available
Performance: Why building HTTP servers matters
HIGH IMPACT
This concept affects how quickly a web page or API responds to user requests, impacting the initial load time and interaction speed.
Serving web content quickly and reliably
Node.js
import http from 'node:http';
import { Worker } from 'node:worker_threads';

const server = http.createServer((req, res) => {
  const worker = new Worker(new URL('./heavyTask.js', import.meta.url));
  worker.on('message', () => res.end('Hello World'));
});
server.listen(3000);
Offloading heavy tasks to worker threads keeps the event loop free, allowing fast responses and smooth interaction.
📈 Performance GainNon-blocking event loop, low response latency, improved INP
Serving web content quickly and reliably
Node.js
const http = require('http');
http.createServer((req, res) => {
  // Synchronous heavy computation
  for(let i = 0; i < 1e9; i++) {}
  res.end('Hello World');
}).listen(3000);
Blocking the event loop with heavy synchronous code delays all responses, causing slow page loads and poor interaction.
📉 Performance CostBlocks event loop, causing high response latency and poor INP
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Blocking synchronous server codeN/AN/ADelays initial paint[X] Bad
Non-blocking asynchronous server with workersN/AN/AEnables fast initial paint[OK] Good
Rendering Pipeline
The HTTP server handles incoming requests and sends responses. Slow server responses delay the browser's ability to start rendering, impacting the critical rendering path.
Network Request
First Byte Time
DOM Construction
⚠️ BottleneckServer response time (Time To First Byte)
Core Web Vital Affected
LCP, INP
This concept affects how quickly a web page or API responds to user requests, impacting the initial load time and interaction speed.
Optimization Tips
1Avoid blocking the event loop with synchronous code in HTTP servers.
2Use asynchronous patterns and worker threads for heavy computations.
3Fast server responses improve LCP and user interaction speed.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance impact of blocking synchronous code in a Node.js HTTP server?
AIt improves browser paint speed.
BIt reduces the bundle size sent to the client.
CIt delays all incoming requests, increasing response time.
DIt decreases network latency.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page, and check the Time To First Byte (TTFB) for the main document.
What to look for: A low TTFB indicates a fast server response, which improves LCP and overall page load speed.