0
0
Node.jsframework~8 mins

Why Node.js for server-side JavaScript in Node.js - Performance Evidence

Choose your learning style9 modes available
Performance: Why Node.js for server-side JavaScript
HIGH IMPACT
This concept impacts server response time and how quickly the server can handle many requests simultaneously.
Handling multiple client requests on the server
Node.js
import { createServer } from 'http';
import { readFile } from 'fs/promises';
createServer(async (req, res) => {
  // Non-blocking async file read
  const data = await readFile('file.txt');
  res.end(data);
}).listen(3000);
Async file reading allows the event loop to handle other requests while waiting for I/O.
📈 Performance GainNon-blocking I/O improves concurrency and reduces response delays
Handling multiple client requests on the server
Node.js
const http = require('http');
http.createServer((req, res) => {
  // Blocking synchronous file read
  const data = require('fs').readFileSync('file.txt');
  res.end(data);
}).listen(3000);
Using synchronous file reading blocks the event loop, delaying all other requests until the file is read.
📉 Performance CostBlocks event loop, causing high response time and poor concurrency
Performance Comparison
PatternEvent Loop BlockingConcurrencyResponse TimeVerdict
Synchronous blocking I/OBlocks event loopLow concurrencyHigh response time[X] Bad
Asynchronous non-blocking I/ONo blockingHigh concurrencyLow response time[OK] Good
Rendering Pipeline
Node.js handles incoming requests using an event loop that processes callbacks without blocking. Non-blocking I/O operations allow the server to continue processing other requests while waiting for slow tasks like file or network access.
Event Loop
I/O Operations
Callback Execution
⚠️ BottleneckBlocking synchronous operations that pause the event loop
Core Web Vital Affected
INP
This concept impacts server response time and how quickly the server can handle many requests simultaneously.
Optimization Tips
1Avoid synchronous blocking calls in Node.js server code.
2Use asynchronous APIs for I/O to keep the event loop free.
3Non-blocking I/O improves server concurrency and response time.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of Node.js using non-blocking I/O?
AIt reduces the size of the server code bundle.
BIt improves the visual layout of the webpage.
CIt allows the server to handle many requests without waiting for slow operations.
DIt automatically caches all server responses.
DevTools: Performance
How to check: Record a server profile while sending multiple requests. Look for long blocking tasks in the event loop.
What to look for: Long blocking periods indicate synchronous code slowing down the server; short, quick callbacks show good async usage.