Node.js › Performance Optimization & Profiling

How would you generate and interpret a CPU profile for a Node.js application using the built-in --prof flag?

Intermediate

Answer

Running 'node --prof app.js' generates a V8 profiling log capturing where CPU time is spent during execution -- afterward, running 'node --prof-process' on the generated log file produces a human-readable summary breaking down time spent in JavaScript code, C++ code (native bindings), and the garbage collector, with a 'Summary' section showing the overall ticks distribution and a 'Bottom up (heavy) profile' section identifying the specific functions consuming the most CPU time.
node --prof server.js
# ... exercise the application under representative load ...
# Ctrl+C to stop, then process the generated isolate-*.log file:
node --prof-process isolate-0x*.log > profile-report.txt
Real-world example A team investigating why their API's CPU usage seemed disproportionately high runs --prof during a load test, discovering from the resulting report that an unexpectedly large percentage of CPU ticks were being spent inside the garbage collector, pointing them toward investigating excessive object allocation rather than assuming the bottleneck was in their own application logic.

Common follow-ups: What does a high percentage of time spent 'in GC' in the profile output typically indicate about the application's allocation patterns?;How does this built-in --prof flag compare in usability to a visual tool like Chrome DevTools' CPU profiler or clinic flame?