CPU profiling
Prerequisite: node-memory-and-heap-snapshots. Objective: Compare measured sampling limitations. Local baseline: Node.js 22.16.0 on Linux, ESM. Each example is independent: save as main.mjs in a disposable directory and run node main.mjs. The author harness uses a five-second timeout. Static snippets are not ByteMind interactive execution.
How it works
Use an inspector Session with Profiler.enable, Profiler.start and Profiler.stop on a bounded synthetic workload. Keep profile data private and disconnect after use. A profile describes sampled stacks; short functions may be missed, sampling overhead exists, and waiting on I/O may not appear as a CPU hot spot. Use monotonic measurements and document runtime, workload, warmup, sample count and errors. Compare alternatives only with correctness checks and representative conditions. This lesson validates profile shape and timing mechanics, not a production speed comparison.
Collect a real bounded CPU profile in process
import {Session} from 'node:inspector';import {promisify} from 'node:util';import assert from 'node:assert/strict';const s=new Session();s.connect();const post=promisify(s.post.bind(s));try{await post('Profiler.enable');await post('Profiler.start');let total=0;for(let i=0;i<200000;i++)total+=i%7;const {profile}=await post('Profiler.stop');assert.ok(Array.isArray(profile.nodes)&&profile.nodes.length>0);assert.ok(profile.endTime>=profile.startTime);console.log('profile nodes available');assert.equal(total,599994);console.log('workload checked');}finally{try{await post('Profiler.disable');}finally{s.disconnect();}}Expected output:
profile nodes availableworkload checkedThe actual CPU profile is collected but its private contents and nondeterministic sample counts are not emitted.
Record elapsed duration without a fabricated fixed value
import {performance} from 'node:perf_hooks';import assert from 'node:assert/strict';function sum(n){let out=0;for(let i=1;i<=n;i++)out+=i;return out;}for(let i=0;i<3;i++)assert.equal(sum(1000),500500);const samples=[];for(let i=0;i<5;i++){const start=performance.now();assert.equal(sum(1000),500500);samples.push(performance.now()-start);}assert.ok(samples.every(x=>Number.isFinite(x)&&x>=0));console.log('warmups 3; samples 5; results correct');Expected output:
warmups 3; samples 5; results correctActual durations are variable and not used as a regression threshold. A later performance chapter develops a documented measurement protocol.
Common mistakes
Do not report a universal speedup from one tiny run or assert exact sample counts. A sampling profile cannot prove every function call occurred.
Practice
Propose a repeatable comparison of two pure implementations, with warmup, equal inputs and output assertions. Record median and variation only after running the benchmark.
Reference candidate: node-cpu-profiling-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/inspector.html, https://nodejs.org/docs/latest-v22.x/api/inspector.html. Exact-version documentation not retrieved in this batch; local fixture behavior tested.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CPU profiling — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Why avoid exact CPU sample count assertions?
What should be checked alongside a benchmark?
Can CPU profiling alone diagnose I/O wait?