The event loop and useful work
Reason about responsiveness instead of assuming async means parallel.
What you’ll learn
- Distinguish waiting for I/O from CPU work
- Identify a blocking callback
- Plan bounded request work
Separate execution from waiting
A server spends time doing different kinds of work. It may calculate a result in JavaScript, wait for data from a socket, or ask another subsystem to perform an operation. Node.js coordinates asynchronous work through an event loop and additional mechanisms whose details depend on the API. An asynchronous interface does not mean that all work happens in parallel, and it does not make expensive JavaScript computation disappear. First identify which part of an operation is actually consuming time.
A long callback affects other requests
Suppose one request enters a JavaScript loop that takes several seconds before returning. Other callbacks that need that same event-loop thread cannot run during that continuous computation. A server can therefore respond poorly even when most of its dependencies expose asynchronous APIs. Bound parsing sizes, avoid unnecessary repeated work, and measure slow paths. Moving CPU work to an appropriate worker or another service can be useful, but introduces coordination and resource limits that also need design.
Trace a request lifecycle
Consider reading a small configuration file and then sending a response. With a nonblocking API, the request handler can register work and return control while the operation is pending. Later, its continuation can process the result. That continuation must still handle failure and avoid doing unbounded work. The request may also have been cancelled by the client. A useful trace records when work is requested, when execution returns, when data becomes available, and when the response is completed.
Timers are scheduling tools
A timer delay is not a reservation for an exact instant of execution. The callback must still be eligible to run and the environment must have an opportunity to execute it. Avoid tests that assert a fragile ordering among unrelated mechanisms or rely on one exact millisecond boundary. Use timeouts to bound waiting, and design the surrounding behavior so a late callback does not update an expired request or a closed resource.
Observe before changing architecture
Measure event-loop delay, request latency, dependency time, and resource usage before assuming the bottleneck. If a slow database dominates, adding workers for JavaScript computation may not help. If a huge synchronous transformation dominates, another network cache may not address the problem either. Build a small load scenario with several concurrent requests and one deliberately slow operation. Explain which work can make progress during the wait, then compare that explanation with the measured behavior.
Try it yourself
A function is declared async but spends two seconds in a tight loop before its first await. Does the declaration make that loop nonblocking?
Show a possible solution
No. The loop still runs synchronously on its executing thread. Reduce or partition the work, or move appropriate CPU-intensive work to a bounded worker-based design.
Check your understanding
Try 11 questions across node.js. Read each explanation and revisit anything that needs another pass.
Practise this subjectGo to the source
Use these primary references to check details and explore the topic further.
Saved in this browser. Signed-in progress syncs to your account.