How do you handle CPU-intensive tasks in Node.js?
Use worker_threads for in-process CPU work, child_process (fork) for separate scripts, or offload to a queue (Bull, SQS) processed by separate workers. Never run CPU-heavy tasks in the main event loop they block all other requests.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Node.js Performance and Scaling Interview Questions Clusters, Streams, and Memory
Use cluster mode (multiple instances per CPU core via PM2 -i max or cluster module), load balancing (Nginx with multiple instances), caching (Redis for hot data), database optimization (indexes, query tuning), CDN for static assets, and microservices for independent scaling.
Streams process data in chunks instead of loading everything into memory. Types: Readable, Writable, Duplex, Transform. Use fs.createReadStream instead of fs.readFile for large files to avoid out-of-memory errors. Pipe connects readable to writable streams.
Monitor process.memoryUsage() periodically. Common causes: global variables accumulating data, event listeners not removed (use EventEmitter.setMaxListeners and off()), closures holding references, and timers not cleared (clearInterval/clearTimeout). Use heap snapshots and Chrome DevTools for debugging.
Still have questions?
Browse all our FAQs or reach out to our support team
