← Back to Node.js, page 4

nodejs-0065 · Hard senior

A Node.js service is slow. How do you decide whether the problem is CPU, the event loop, the thread pool or a slow dependency?

Check event loop utilization (ELU) and event loop delay first. High ELU with high delay means JavaScript is busy: profile the CPU. Low ELU with slow responses means the process is waiting: look at downstream calls, the thread pool, connection pools and locks. Growing memory points to heap snapshots, and high garbage collection time to allocation pressure.

Full answer

Do not guess. Use a short decision path that mirrors what tools such as clinic doctor report.

Step 1: measure.

  • Event loop utilization (performance.eventLoopUtilization()): the share of time the loop was running JavaScript instead of waiting. Near 1 means saturated.
  • Event loop delay (monitorEventLoopDelay): how late timers fire.
  • CPU usage of the process (process.cpuUsage(), or the host metric) and memory (process.memoryUsage()).
  • Latency percentiles per route from your metrics.

Step 2: classify.

  • High ELU, high delay, high CPU: JavaScript-bound. Take a CPU profile and fix the hot function, or move work to worker threads.
  • Low ELU, low CPU, high latency: waiting. The time is spent in a database, another service, a lock, a connection pool queue, or DNS. Add timing around outgoing calls and check pool sizes. Also check thread pool saturation (fs, crypto, dns.lookup tasks queueing).
  • High CPU but low ELU: work outside the main thread, such as garbage collection, the thread pool (hashing, compression) or worker threads. Check --trace-gc and the thread pool use.
  • Memory climbing: take heap snapshots. Frequent long GC pauses: reduce allocation or the retained set.
  • Latency spikes at regular intervals: look for timers, cron jobs, cache expiry or GC.

Step 3: change one thing, measure again with the same load (autocannon or k6).

The point is the order: cheap metrics first, a profiler second, and code changes last.

javascript
const { performance } = require('node:perf_hooks');
const { setTimeout: sleep } = require('node:timers/promises');

// Measure ELU over one piece of work and say what kind of problem it looks like.
async function diagnose(label, work) {
  const start = performance.eventLoopUtilization();
  await work();
  const { utilization } = performance.eventLoopUtilization(start);
  const verdict =
    utilization > 0.8 ? 'JavaScript is busy: take a CPU profile'
    : utilization < 0.2 ? 'mostly waiting: check dependencies, pools and the thread pool'
    : 'mixed: measure per route';
  console.log(label.padEnd(26), '->', verdict);
}

function busy(ms) {
  const end = performance.now() + ms;
  while (performance.now() < end) { /* spin */ }
}

async function main() {
  await sleep(20); // let start-up settle

  await diagnose('slow database call', () => sleep(300));
  await diagnose('heavy calculation', async () => { await sleep(0); busy(300); });
  await diagnose('half and half', async () => {
    for (let i = 0; i < 5; i++) { busy(30); await sleep(30); }
  });
}

main();

The three cases mimic a slow database call, a CPU-heavy handler and a mix. Real services should report ELU as a metric and alert on it. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): slow database call -> mostly waiting: check dependencies, pools and the thread pool heavy calculation -> JavaScript is busy: take a CPU profile half and half -> mixed: measure per route

Key points

  • ELU and event loop delay separate busy from waiting.
  • High ELU: profile the CPU. Low ELU with slow responses: look at dependencies and pools.
  • Heap growth: snapshots. GC pressure: reduce allocation.
  • Change one thing and measure under the same load.

Common mistakes

  • Guessing and optimising the code before measuring.
  • Treating high latency as a CPU problem when the loop is idle.
  • Looking at average CPU only and missing one saturated core.
  • Ignoring the thread pool when fs, dns or crypto calls queue.

Follow-ups

  • What does clinic doctor report, and how does it get its data?
  • How would you tell if the thread pool is the bottleneck?
  • What metric would you alert on for the event loop?

Tips

  • Give the decision path in order.
  • Name ELU as the first number you check.
  • Add that you change one thing at a time.