September 13, 2022 in #coding
🟢 profiling node scripts
Node supports generating data from scripts that is required to create a flamegraph. To start profiling a script, run it with the
inspect command to begin a debugging session.
node inspect index.js
You'll see that Node starts an interactive debugging session, halted at the first line of your script. Here you can run the script as many times as you need with features like breakpoints and step by step execution. Type
help at the
debug> prompt to see all available options.
To begin profiling, click the blue Start button at the bottom of the Profiler tab. Then go to your terminal and type
cont to continue your script execution. Once completed, go back to the Profiler tab and click Stop.
Then you'll see an interactive flamegraph that you can use to view which function calls took the most time during the execution. It can be viewed as a chart, a tree, or a list, but I prefer the chart.
Between each profiling run, you must clear all profiles in the Profiler tab and restart your Node debugger with the
While I found the restart process to be a little inconvenient, it was the most reliable way to get a profile capture across multiple runs of my script.