Performance: fork for Node.js child processes
MEDIUM IMPACT
This affects how Node.js handles parallel tasks and CPU-intensive operations without blocking the main event loop.
const { fork } = require('child_process');
const child = fork('heavyTask.js');
child.on('message', (result) => {
console.log(result);
});
child.send('start');const result = heavyComputation(); // runs in main process synchronously
console.log(result);| Pattern | CPU Usage | Event Loop Blocking | IPC Overhead | Verdict |
|---|---|---|---|---|
| Synchronous heavy task in main process | High CPU on main thread | Blocks event loop fully | None | [X] Bad |
| Using forked child process for heavy task | Distributed CPU load | No blocking on main thread | Moderate IPC overhead | [OK] Good |