Performance: Why Node.js for server-side JavaScript
This concept impacts server response time and how quickly the server can handle many requests simultaneously.
Jump into concepts and practice - no test required
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);
const http = require('http'); http.createServer((req, res) => { // Blocking synchronous file read const data = require('fs').readFileSync('file.txt'); res.end(data); }).listen(3000);
| Pattern | Event Loop Blocking | Concurrency | Response Time | Verdict |
|---|---|---|---|---|
| Synchronous blocking I/O | Blocks event loop | Low concurrency | High response time | [X] Bad |
| Asynchronous non-blocking I/O | No blocking | High concurrency | Low response time | [OK] Good |
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello World');
});
server.listen(3000, () => console.log('Server running'));const http = require('http');
const server = http.createServer((req, res) => {
res.write('Hello');
res.end();
});
server.listen(3000);
console.log('Server running on port 3000');