liveBook · Manning

Event loop delay

As explained in JavaScript in Depth

A measure of the time queued tasks wait before a JavaScript runtime begins executing them, excluding the tasks’ own execution time.

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

How JavaScript in Depth covers it

How deep it goes
Defines queue wait time, explains timer-based sampling and percentiles, then gives a GC-and-CPU-profile diagnostic path without identifying causes from delay alone.
Examples
Uses a 20 ms timer firing at 25 ms, a p50–p99 contrast, and a Node.js monitoring snippet.
What it assumes
Introduces the metric through a timer; assumes familiarity with callbacks, asynchronous operations, and percentiles.
What you can do after
Diagnose/debug with it by measuring p99 delay, checking GC correlation, and profiling remaining main-thread work.
How it connects
Connects event loop delay to I/O latency, garbage collection, CPU profiling, synchronous JavaScript, and microtask queues.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.