advanced

Profiling

Use measurements such as event-loop delay, CPU profiles, heap snapshots, flamegraphs, and production-safe sampling.

Profile with measurements, not guesses — Node offers several production-safe tools:

| Tool | Reveals | |------|---------| | `perf_hooks.monitorEventLoopDelay` | Stall time blocking I/O and timers | | `node --cpu-prof` / clinic.js | Hot JS functions | | Heap snapshot (`heapdump`) | Retained object graphs | | Async hooks / tracing | Slow async operations |

					import { monitorEventLoopDelay } from 'node:perf_hooks';
const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
// after load...
console.log(h.mean / 1e6, 'ms mean delay');
				

Sample in staging at realistic QPS before enabling heavy profilers in prod. Flamegraphs show width = time — chase plateaus, not single narrow spikes first.

On interviews: how you diagnosed event loop blockage vs memory vs external dependency; safe prod profiling practices.

Common pitfalls: optimizing cold paths; taking one snapshot under idle process; ignoring GC pauses.

The trade-off is balancing simplicity, performance, safety, and operability — name which axis you optimized and what cost you accepted.

Checklist:

  • Baseline metrics before change.
  • Reproduce load in staging.
  • Pair CPU profile with loop delay.
  • Document findings and regression guard.