Performance: Worker pool pattern
HIGH IMPACT
This pattern affects how efficiently CPU-intensive tasks are handled without blocking the main event loop, improving responsiveness and throughput.
const http = require('http'); const { Worker } = require('worker_threads'); function runHeavyTask() { return new Promise((resolve, reject) => { const worker = new Worker('./heavyTask.js'); worker.on('message', resolve); worker.on('error', reject); }); } http.createServer(async (req, res) => { const result = await runHeavyTask(); res.end(`Result: ${result}`); }).listen(3000);
const http = require('http'); http.createServer((req, res) => { // Heavy computation directly on main thread let result = 0; for (let i = 0; i < 1e9; i++) { result += i; } res.end(`Result: ${result}`); }).listen(3000);
| Pattern | CPU Usage | Memory Usage | Main Thread Blocking | Verdict |
|---|---|---|---|---|
| Heavy tasks on main thread | High CPU on main thread | Low memory | Blocks main thread causing high INP | [X] Bad |
| Unlimited workers for each task | High CPU across many threads | High memory usage | Less main thread blocking but high overhead | [!] OK |
| Worker pool with limited threads | Balanced CPU across workers | Controlled memory usage | No main thread blocking, smooth responsiveness | [OK] Good |