0
0
Node.jsframework~10 mins

CPU profiling basics in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CPU profiling basics
Start Node.js app
Enable CPU profiler
Run workload
Collect CPU profile data
Analyze profile
Identify slow functions
Optimize code
Repeat profiling if needed
This flow shows how CPU profiling works: start the app, enable profiling, run code, collect data, analyze to find slow parts, then optimize.
Execution Sample
Node.js
import * as inspector from 'node:inspector';

const session = new inspector.Session();

inspector.open(9229);

await session.connect();

await session.post('Profiler.enable');
await session.post('Profiler.start');

// run code

const { profile } = await session.post('Profiler.stop');

console.log(profile);
This code starts the CPU profiler, runs some code, stops profiling, and logs the collected CPU profile data.
Execution Table
StepActionProfiler StateCPU Usage DataOutput
1Start Node.js appProfiler inactiveNo dataApp running
2Enable CPU profilerProfiler activeCollecting dataProfiler started
3Run workloadProfiler activeRecording CPU samplesWorkload executing
4Stop CPU profilerProfiler inactiveFinal CPU profile collectedProfile data ready
5Analyze profileProfiler inactiveProfile data analyzedIdentified slow functions
6Optimize codeProfiler inactiveNo data changeCode improved
7Repeat profiling if neededProfiler activeNew data collectedCycle continues
💡 Profiling stops when profiler is disabled or app ends
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
profilerStateinactiveactiveinactiveinactiveinactive
cpuProfileDatanonecollectingcollectedanalyzedanalyzed
Key Moments - 3 Insights
Why does the profilerState change from active to inactive after stopping?
Because stopping the profiler disables data collection, so profilerState changes to inactive as shown in execution_table step 4.
What does cpuProfileData contain after stopping the profiler?
It contains the full CPU usage profile collected during the workload, ready for analysis as shown in execution_table step 4.
Why do we need to repeat profiling after optimizing code?
To verify if the optimizations improved performance by collecting new CPU data, as shown in execution_table step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the profilerState at Step 3?
Ainactive
Bactive
Cstarting
Dstopped
💡 Hint
Check the 'Profiler State' column at Step 3 in the execution_table.
At which step does the CPU profile data become fully collected?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'CPU Usage Data' column to find when data collection finishes.
If we skip Step 6 (Optimize code), what happens at Step 7?
ANew profile data will still be collected
BProfiler will remain inactive
CNo new data will be collected
DApp will crash
💡 Hint
Step 7 shows profiler active and new data collected regardless of optimization.
Concept Snapshot
CPU Profiling Basics in Node.js:
- Start profiler before workload
- Run code to collect CPU samples
- Stop profiler to get profile data
- Analyze to find slow functions
- Optimize code and repeat profiling
Use node:inspector Profiler API for control.
Full Transcript
CPU profiling in Node.js involves starting the profiler before running your code, collecting CPU usage data during execution, then stopping the profiler to get the full profile. This profile shows which functions use the most CPU time. You analyze this data to find slow parts of your code. After optimizing those parts, you can profile again to check improvements. The profiler state changes from inactive to active when started, and back to inactive when stopped. CPU profile data is collected only while the profiler is active. This process helps improve app performance by focusing on real CPU usage.