Take Advantage of the Built-in Profiler
Performance is very important to any system. Profiling your code will allow you to understand where time is spent in your code, so you can focus on the right pieces of code that can most benefit from performance optimization.
V8’s internal profiler comes built-in with Node.js. It’s easy-to-use and very efficient.
Let’s take a look at how to use this profiler.
Running your application with profiling enabled
It’s very easy to start using profiler; simply add the
--prof option to your node command:
node --prof app.js
Now let your task run, do all the HTTP calls or whatever needs to be done to execute your code.
When finished, the profiler will create a “tick file”; a big text file in your current working directory named
Next, we need to process this file.
Processing your tick file
To process your tick file, simply run the following command:
node --prof-process isolate-0x???????-v8.log
This command will output the profiling result of your app, which consists of several parts (discussed further in the next section).
As this result can be a bit long, it is advised to pipe it into a text file.
Reading the tick file processing result
The processed result will contain several parts, including:
[C++ entry points]
[Bottom up (heavy) profile]
Jump directly to the
[Summary] part. It is short and gives an immediate overview of where to dig further. For example:
ticks total nonlib name
7630 29.4% 29.9% C++
966 3.7% 3.8% GC
496 1.9% Shared libraries
2638 10.2% Unaccounted
Date objects, or overuse of the
Object.keys() function, both of which can adversely affect C++ performance.
Guided by the above summary information, you can then jump to the sections where most of the time is being spent. You will find a list of functions sorted by the number of ticks spent on each, in descending order.
You now know where to focus your efforts.