liveBook · Manning

Sample-based profiling

As explained in JavaScript in Depth

The technique of periodically observing a running program to estimate the relative cost of its execution activities.

Sample-based profiling estimates execution cost by interrupting a running program at regular intervals, recording its call stack, and resuming execution. Aggregated samples show where the program spent time: a function holding 25% of samples occupied the stack for roughly 25% of the recording. Because this is statistical measurement, it records neither every function call nor exact call counts. section 11.1.6

Interpret profiles according to the layer being measured:

  • V8's --prof includes JavaScript execution, C++ runtime code, garbage collection, compilation, and shared libraries, then requires processing the raw log. section 11.1.6
  • --cpu-prof produces a Chrome DevTools-compatible profile with JavaScript function names, source locations, and sample counts, but can omit or misattribute work outside the JavaScript call stack. section 11.1.6

Collecting a V8 profile

Run `node --prof server.js`, then process the resulting isolate log with `node --prof-process isolate-0x*.log` to obtain summary, bottom-up, and top-down reports. section 11.1.6

node --prof server.js

The command listing provides a directly runnable entry point for collecting a V8 tick-based profile. section 11.1.6

How JavaScript in Depth covers it

How deep it goes
Explains periodic stack sampling, aggregation, statistical interpretation, and runtime-specific visibility limits.
Examples
Uses Node.js --prof collection, --prof-process conversion, and Chrome DevTools-compatible --cpu-prof output.
What it assumes
Assumes familiarity with running JavaScript programs and reading call stacks while defining sampling-based cost estimation.
What you can do after
Diagnose/debug with Node.js profiling commands by collecting samples and reading aggregated function-time reports.
How it connects
Connects CPU profiling with call stacks, JavaScript runtimes, garbage collection, compilation, and C++ runtime code.

Also explained in

Compare how 2 books explain sample-based profiling →

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.