Performance: Worker thread vs child process
HIGH IMPACT
This concept affects how Node.js handles CPU-intensive tasks and parallelism, impacting responsiveness and throughput.
const { Worker } = require('worker_threads');
const worker = new Worker('./heavyTask.js');
worker.on('message', (result) => {
console.log(result);
});const { fork } = require('child_process');
const child = fork('heavyTask.js');
child.on('message', (result) => {
console.log(result);
});| Pattern | Memory Usage | Startup Time | Communication Overhead | Verdict |
|---|---|---|---|---|
| Child Process | High (~5-10MB per process) | Slow (~10-50ms) | High (IPC via serialization) | [!] OK |
| Worker Thread | Lower (~1-5MB per thread) | Fast (~1-5ms) | Lower (Shared memory possible) | [OK] Good |