Skip to content

Lesson 8 of 9 · 26m

CPU Profiles, Flamegraphs, and Finding the Hot Stack

Capture CPU evidence with the inspector and clinic-compatible workflows, interpret self versus total time, and avoid optimizing the wrong function.

Performance tuning without a profile is just superstition with better formatting. Node gives you unusually good tooling: the V8 inspector, CPU profiles, trace events, and flamegraph tooling. The practitioner's skill is not collecting a profile — it is reading one without confusing library bookkeeping for application cost.

Capture a profile from a running process

Start the inspector without exposing it to the world:

node --inspect=127.0.0.1:9229 server.js

For an existing Linux process, kill -USR1 "$NODE_PID" enables the inspector on the default host/port in many Node versions. Connect through Chrome DevTools or SSH-forwarded local tooling; never bind the inspector publicly because it is effectively a debugging control plane.

A repeatable local capture

The simplest deterministic method is:

node --cpu-prof --cpu-prof-dir=/tmp/profiles --cpu-prof-name=api.cpuprofile server.js

Drive realistic traffic, stop the process cleanly, and inspect /tmp/profiles/api.cpuprofile. If startup cost matters, --cpu-prof catches it. If only steady-state matters, capture after warm-up so JIT compilation noise does not dominate.

Self time versus total time

A flamegraph width usually represents time spent somewhere in a stack, not necessarily inside the top function. Learn the distinction:

self time    CPU attributed directly to that function
total time   self plus everything it called
hot stack    repeated path through callers and callees

JSON.parse may have high self time, but the important question is which endpoint keeps parsing oversized bodies. A framework router may have high total time simply because every request passes through it.

Watch for hidden runtime costs

Common hot-path offenders:

JSON.parse(largeString)
JSON.stringify(largeObject)
crypto.pbkdf2Sync(password, salt, 310000, 32, 'sha256')

Also look for repeated regex compilation, template rendering, object cloning, compression, serialization in logging, and ORM hydration. Logging frameworks are frequent offenders because they sit on every path and format objects eagerly.

Async stacks can mislead

Node async stack stitching has improved, but profiles still fragment across event boundaries. You may see worker completion, timer execution, and promise continuations as separate roots. Correlate by time window and application markers rather than assuming one continuous request stack. Distributed traces can complement, not replace, CPU profiles.

Optimize the shape, not only the function

The best fix is often to do less work: cache canonical output, bound input size, stream instead of buffering, replace quadratic diffing, reuse compiled validators, or move truly parallel CPU work to worker_threads. After the fix, compare equivalent profiles under equivalent load. A single benchmark run is marketing, not evidence.

Field notes

  • JIT warm-up changes early profiles. Discard startup captures unless startup latency is the target.
  • Inlining can make stack names surprising; follow the shape and source locations rather than expecting your function names everywhere.
  • Profilers add overhead. Compare relative hot spots inside the same capture, not absolute production throughput while profiling.
  • Never expose --inspect on 0.0.0.0; use SSH forwarding or controlled local access.

Was this lesson helpful?

Sign in to rate