A request can finish its I/O yet still wait before JavaScript handles the result. Event loop delay is the time ready tasks spend waiting for the runtime to begin executing them—not the time those tasks take to execute. A slow callback and a callback that starts late can both increase response time, but they call for different investigations. section 11.1.8 section 11.1.9
A timer that fires late
Set a timer for 20 ms. If its callback starts after 25 ms, the observed delay is 5 ms. This is an imprecise probe: it shows that the loop did not reach the timer on schedule, not which operation held it up. One long synchronous operation, many shorter operations, or garbage-collection pauses could produce the delay. section 11.1.8
const { monitorEventLoopDelay } = require('node:perf_hooks');
const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
setInterval(() => {
console.log(`p50: ${(h.percentile(50) / 1e6).toFixed(1)} ms ` +
`p99: ${(h.percentile(99) / 1e6).toFixed(1)} ms`);
h.reset();
}, 5000);In Node.js, monitorEventLoopDelay() samples timer lateness at a configured resolution. Its histogram reports nanosecond values; convert them to milliseconds before reading p50 and p99. Resetting after each report makes successive intervals comparable. A low p50 alongside a high p99 exposes occasional stalls that an average could obscure. section 11.1.8
When p99 rises, use the delay as a symptom, then look for its cause:
- Compare spikes with --trace-gc. Matching garbage-collection pauses point toward GC pressure. section 11.3.1
- If GC does not explain them, inspect a CPU profile. One dominant function suggests an expensive synchronous operation; no dominant function can indicate accumulated short operations or microtask draining. section 11.3.1
- Check JSON parsing or serialization and promise microtask execution alongside GC, but do not treat delay alone as proof of any one cause. section 11.1.8
Wall-clock timing around fetch() also includes time waiting for its callback, so it cannot isolate network latency from event loop delay. Conversely, monitorEventLoopDelay() does not capture time spent draining microtasks within a macrotask turn, a blind spot for promise-heavy workloads. The API is available through Deno’s Node.js compatibility layer, though its event loop uses Tokio rather than Node.js’s libuv; Bun support is uncertain in the supplied evidence. section 11.1.9 section 11.6 section 11.1.8
