CPU profiling helps you see which parts of your Node.js program use the most processor time. This helps find slow spots to make your app faster.
0
0
CPU profiling basics in Node.js
Introduction
When your Node.js app feels slow or laggy
To find which functions take the most time during execution
Before optimizing code to know where to focus
To compare performance before and after changes
When debugging performance issues in production or development
Syntax
Node.js
node --prof your_script.js // After running, process the log: node --prof-process isolate-0xNNNNNNNNNN-v8.log > processed.txt
Use --prof flag to start CPU profiling in Node.js.
After running your script, use --prof-process to convert the raw log into readable output.
Examples
Start CPU profiling while running
app.js.Node.js
node --prof app.js
Convert the raw CPU profile log into a readable summary file.
Node.js
node --prof-process isolate-0x1234567890-v8.log > summary.txt
Sample Program
This simple program runs a slow function that sums numbers from 0 to 10 million. You can run it with node --prof to see where the CPU time is spent.
Node.js
function slowFunction() {
let sum = 0;
for (let i = 0; i < 1e7; i++) {
sum += i;
}
return sum;
}
console.log('Starting slow function...');
const result = slowFunction();
console.log('Result:', result);OutputSuccess
Important Notes
CPU profiling adds some overhead, so your program may run slower while profiling.
Always process the raw log file with --prof-process to understand the results.
Use Chrome DevTools or other tools to visualize the processed profile for easier analysis.
Summary
CPU profiling shows where your Node.js app spends most of its processing time.
Use node --prof to record and node --prof-process to analyze.
This helps find slow code to improve app speed.