Diagnosing a Slow Node.js Service with Event Loop Measurements

A request takes a quarter of a second. The CPU graph looks fairly relaxed. Somewhere, a dashboard has decided that the service is healthy.
The request is still slow.
A latency number tells us what the caller experienced, but it doesn't identify where the time went. In Node.js, a busy JavaScript thread, a queue of worker‑pool jobs, and a delayed upstream response can all leave a request waiting. They need different investigations, and usually different fixes.
I use a small fault‑injection service to make those differences visible. It runs the same route under three deliberately awkward conditions, measures the event loop, and sends two independent probes alongside the ordinary requests. The faults are artificial, which is rather useful when the point is knowing exactly what caused them.
Ask a Small Question Alongside the Slow One
My ordinary canary route simply returns a short response. It doesn't read a file, look up a hostname, or perform cryptography. Its job is to tell me whether the server can receive and handle something cheap whilst the main route is busy.
The second probe performs a tiny asynchronous pbkdf2 operation. That makes it deliberately dependent on libuv's worker pool. Its input is fixed, its iteration count is one, and it is not a password‑storage recommendation. I want a small job whose queueing time becomes conspicuous when expensive jobs occupy the pool.
The load driver runs in a separate process. Putting it on the server's blocked JavaScript thread would delay the thing supposed to measure the delay. That would make for an unusually cooperative benchmark.
I discussed the basic scheduling model in my event loop guide. Here, the useful question is more specific: can the service make progress on unrelated work whilst this route is waiting?
Measure a Window, Then Reset It
Node's perf_hooks module supplies a delay histogram and an event loop utilisation measurement. In the Node 16.15 fixture, I start with:
const histogram = monitorEventLoopDelay({ resolution: 10 });
histogram.enable();
let baseline = performance.eventLoopUtilization();Before each measured window, I reset the histogram and take a fresh utilisation baseline. After the window, I read a delta rather than comparing cumulative values from processes with different lifetimes:
const utilisation = performance.eventLoopUtilization(baseline);
const delayP95Ms = histogram.percentile(95) / 1e6;The Node 16 performance API documentation gives the units and meanings. The sampling resolution is in milliseconds; histogram delays are reported in nanoseconds. Missing that conversion produces some impressively alarming graphs.
The histogram measures scheduling delay through a timer. It is not a direct measurement of every request's waiting time, and the configured sampling interval affects what it can show. I keep the resolution unchanged across comparisons and record it with the results.
Event loop utilisation is also not process CPU utilisation. My fixture records both. Work running in other threads can consume CPU whilst the event loop remains available, and work which keeps the loop occupied need not translate neatly into the same percentage on a host‑level CPU chart.
Three Faults, Three Different Signatures
The blocking case calls pbkdf2Sync inside the request handler. The pool case uses asynchronous pbkdf2 with the same inputs and iteration count. The upstream case waits for an HTTP response from another local process, which deliberately delays its reply.
I set the pool size before starting the service and keep it at two workers for every case. The driver uses four concurrent requests, twelve requests per window, a warm‑up request, and three rounds. These are small diagnostic runs, not a throughput benchmark.
The observed pattern is more useful than any single timing:
| Fault | Ordinary canary | Pool probe | Event loop |
|---|---|---|---|
| Synchronous cryptography | Delayed | Delayed too | Busy, with longer scheduling delays |
| Asynchronous cryptography | More responsive | Delayed behind pool work | Available for much of the window |
| Delayed upstream HTTP | Responsive | Responsive | Mostly waiting |
The synchronous case delays both probes because their callbacks still need the JavaScript thread. A slow pool probe alone therefore doesn't prove pool contention. The ordinary canary is what helps separate the first two cases.
In the pool case, changing the function to an asynchronous API has freed the event loop, but has not made the cryptographic work disappear. Node's guide to avoiding event loop and worker‑pool blocking explains which APIs share that pool. Other users of the same resource can end up waiting behind a route which appears perfectly asynchronous in a code review.
Follow the Evidence to the Next Investigation
When the ordinary canary slows and the loop becomes busy, I would inspect synchronous handlers, expensive parsing, large transformations, and long callback chains. A CPU profile can help locate the work. Capture a representative interval and account for the profiler's own overhead rather than leaving instrumentation changes out of the comparison.
When the pool probe slows whilst the ordinary canary stays responsive, I would inspect the volume and duration of pool‑backed operations. Bound the work admitted to the expensive route before assuming that more threads will solve it. Increasing UV_THREADPOOL_SIZE changes concurrency and resource demand; it doesn't increase the CPU allowance of a constrained container.
When both probes remain responsive but the route is slow, look at the dependency path. Break out connection establishment, remote response time, application retries, and any queue before the request starts. My synthetic upstream has one known delay. A real dependency may have several, and a healthy event loop doesn't identify which one matters.
My browser and Node.js comparison covers why sharing JavaScript syntax doesn't imply sharing the same host facilities. A browser scheduling explanation on its own isn't enough to diagnose this server's worker pool.
Keep the Measurements Honest
My event loop diagnosis lab retains individual request timings, sample counts, histogram summaries, runtime details, and the configured workload. It checks whether the controlled signatures separate across repeated windows. Those relative checks are acceptance tests for the injected faults, not proposed production alert thresholds.
Twelve requests give a very small tail sample. Reporting a percentile does not magically make it a reliable estimate of a busy service's long‑term behaviour. Likewise, comparing a cold process with a warmed one, changing concurrency halfway through, or collecting one case during a noisy build can produce a persuasive explanation for the wrong cause.
Repeat the run, retain the raw observations, and change one relevant condition at a time. Check the process's resource limits as well as the host's apparent spare capacity. A service can be short of its allocated CPU whilst the machine around it looks comfortable.
The point of these probes is to narrow the next question. Once they have done that, investigate the particular queue or operation they identified. A single "Node is slow" graph cannot do that job for us.
Postscript
November 2026: I've rerun my lab on Node 22.23.5 alongside the Node 16.15.0 example. The three fault patterns still separate, but the timings differ, so my download keeps the runtime and workload details with each result. Use a supported runtime for a new service; I use the older runtime only to check the article's original API context.