Node.js Developer interview question · Most asked
Why is CPU-bound work a problem in Node.js and how do you handle it?
Summary
JavaScript runs on one main thread, so a long synchronous computation (large JSON parsing, image processing, crypto, heavy loops) blocks the event loop: no other requests, timers or I/O callbacks run until it finishes.
Longer Answer
Solutions: move work to worker_threads, spawn child processes, offload to a job queue or a service in another language, use async/native implementations (e.g., crypto.scrypt async, sharp for images), or break work into chunks with setImmediate. Monitor event loop lag (perf_hooks.monitorEventLoopDelay) to detect blocking.
Key Points
- Blocks all concurrent requests
worker_threadsfor CPU tasks- Job queues or separate services
- Monitor event loop delay
More most-asked Node.js Developer questions
- What are streams in Node.js and what is backpressure?
- What are the differences between CommonJS and ES modules in Node.js?
- What is the difference between `process.nextTick`, Promise microtasks, `setImmediate` and `setTimeout(fn, 0)`?
- Explain the phases of the Node.js event loop.
- What is Node.js and why is it well suited to I/O-heavy servers?
- How has asynchronous code evolved in Node.js: callbacks, Promises and async/await?
- Walk through how an Express app handles a request, including errors from async handlers.
- How does middleware work in Express?