If you're looking to improve the performance of a Node.js script, a flamegraph is a great way to visualize where time is spent during execution.
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.
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.
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.