Profile Node.js performance with the Performance panel

Nancy Li
Nancy Li
Dale St. Marthe
Dale St. Marthe

Use the Performance panel to profile the performance of Node.js and Deno applications.

What's a CPU profile?

A CPU profile is a report that shows how the CPU was used over a period of time. It can show which programs were using the most CPU time, which processes were running, and how much time was spent in each state. With CPU profiles, you can identify performance bottlenecks and optimize CPU resource utilization.

Open DevTools for Node

  1. In the command line, run:

    Node.js

    node --inspect file-name.js
    

    Deno

    deno --inspect file-name.js
    
  2. Connect to DevTools for Node in one of the following ways:

    • Open DevTools and click the green Node button in the DevTools action bar at the top.
    • In the address bar enter chrome://inspect, then click one of the following:

      • Open dedicated DevTools for Node under Devices.
      • Inspect under the target you want to profile.

    All the ways to open DevTools for Node.

Profile the CPU

To profile the CPU, open the Performance panel and click the radio_button_checked Record button two times to start and end profiling.

The Record button and the VM instance selector.

Analyze profiling results

After you stop the recording, the Performance panel organizes and displays data about the recording in a "profile". Use the following tabs to analyze the profiling data:

  • Timeline overview. Located at the top under the activity bar. Shows CPU and NET activity charts on a timeline. Use it to identify performance bottlenecks.

    The Timeline overview.

  • Bottom-Up: Use this tab to inspect a selected portion of the recording and see aggregated time spent on individual activities.

    The Bottom-Up tab.

  • Call Tree: This tab displays the root activities of a selected portion of the recording. Root activities also have their call stacks nested. Use this tab to identify which activity is causing the most work.

    The Call Tree tab.

  • Event Log: This tab lists activities from a selected portion of the recording in the order that they occurred.

    The Event Log tab.

Profile with the console.profile() command

DevTools lets you profile JavaScript CPU performance with the console.profile() command. You can add this command to your code and then run the file, or copy and paste your code into the Console. The Performance panel will show you the results.

To use this command, follow these steps:

  1. Enclose your code with console.profile() and console.profileEnd(), for example:

    console.profile( profile ${i} );
    // Code to be profiled
    doSomething();
    console.profileEnd();
    
  2. Run your code in one of two ways:

    • If you're using the Console, open DevTools for Node, paste your code to the Console, and press Enter.

    • In the command line, run:

      Node.js

      node --inspect file-name.js
      

      Deno

      deno --inspect file-name.js
      

      Then open DevTools for Node.

Once the profile is completed, the result will be shown in the Performance panel automatically.