15

I can successfully connect the VS Code debugger to my remote NodeJS target using the Chrome protocol. I believe that same protocol supports profiling and performance measurements as it is easy to do from Chrome Dev Tools, but I am not sure it is something I can do from VS Code directly.

Is there any support for this feature ? How can I use it ?

Magix
  • 3,557
  • 3
  • 18
  • 43
  • VSCode goal is to be a text editor, not an IDE. However you can develop an extension that do that (probably there are some already) – Chayim Friedman Apr 18 '20 at 18:32

3 Answers3

16

There is no plugin/support that I am aware of for initiating the profiling, or heap dumps, etc that are available in Chrome's Dev Tools. But, the VS Code debugger can work alongside the dev tools debugger.

Start from VS Code, and start debugging as you would. Then open dev tools from any tab in the Chrome/Chromium browser, and look for the green icon indicating a node.js debugging process is running (manually done via node --inspect):

enter image description here.

Click this green icon and you'll have many of the browser dev tools features for debugging the node.js process, specifically the memory and profiler tabs.

adamrights
  • 1,596
  • 1
  • 10
  • 26
  • This would force me to use a chrome/chromium browser, which can be quite a condition on some systems. Thanks for the answer ! – Magix Feb 23 '19 at 16:51
  • 1
    Well, you can run commands to write memory data, etc that dev tools is hooking into, and then write that to file, and analyze it in some other toolset that you'd probably have to write a bit of a transformer too. So possible, but not nearly as plug and go. – adamrights Feb 25 '19 at 21:11
  • Don't see the green NodeJS icon. The two buttons to the left are there ("Select an element" and "Toggle device bar"), but no green button. Chrome `87.0.4280.88`. Do I need an extension? – cheesus Dec 17 '20 at 13:06
15

Visual Studio Code 1.45 (April 2020) should help, as it integrates Javascript debugging capabilities, including profiling:

New JavaScript debugger

This month we've continued making progress on our new JavaScript debugger.
It's installed by default on Insiders, and can be installed from the Marketplace in VS Code Stable.

You can start using it with your existing launch configurations by enabling the debug.javascript.usePreview setting.

Here are some new features added this month:

Profiling Support

You can capture CPU profiles from your Node.js or browser applications by clicking the new "Profile" button in the Call Stack view, or using the Debug: Take Performance Profile command.

Once you do, you can choose how long the profile will run: until you stop it, for a length of time, or until you hit another breakpoint.

After the profile ends, it's saved in your workspace folder and visualized in VS Code.
When you open the profile, code lenses are added to your files containing performance information at a function level and for certain 'hot' lines.
Unlike profiles captured in many other tools, the recorded profile is sourcemap-aware.

JS Debug profiling


cheesus mentions in the comments having an problem with the line numbers in the profiler output.
Hence microsoft/vscode-js-debug issue 559:

Turns out this is because that location is inside a function that only exists in the compiled code.
We actually do try to source-map it back into the original file, but there's no mapping at that location to use.

https://camo.githubusercontent.com/24b6e6f18cade2b5c8a621ded5241dc752896d00/68747470733a2f2f6d656d65732e706565742e696f2f696d672f32302d30372d38376265353862372d343530622d346334322d613233302d3266306532376336356234322e706e67

If you change your target to es6 or newer -- which you can do nowadays unless you're targeting Internet Explorer or Node versions <= 4 -- TS does not need to generate this stuff and line numbers work.
Your code will also run faster. Here's the latest mappings

This debugger only supports Node 8 and above.
Due to the fact that this bug is only present when targeting old JavaScript versions when using in-place transpilation on code compiled for a version of Node older than what the debugger itself supports, and the complexity involved in fixing this, I will close this issue as out-of-scope.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Could have posted a comment and I would have updated the answer. OP downvoted and removed accepted answer before saw this change (which as you note is April 2020) – adamrights May 04 '20 at 10:49
  • @adamrights I am sorry for the downvote, that was not warranted indeed. – VonC May 04 '20 at 12:04
  • This is great, thanks for the hint. However, the line numbers in the profiler output are wrong. Since I have many anonymous methods, most of them don't appear with a name, and so the profiler output is mostly useless for me. Do you get accurate line numbers? – cheesus Jul 07 '20 at 15:17
  • @cheesus I do not indeed. Maybe there is an issue already opened on that particular bug? – VonC Jul 07 '20 at 15:23
  • I filed a bug https://github.com/microsoft/vscode-js-debug/issues/559. Turns out, the reason (in my case) was ts-node with target `es5`. With `es6`, the line numbers are correct. – cheesus Jul 10 '20 at 07:54
  • 1
    @cheesus Thank you. Great feedback. I have included your comment and issue in the answer for more visibility. – VonC Jul 10 '20 at 10:06
8

Yes, There are many ways.

go to package.json and set:

  { 
    \\ other information 

    "scripts": {
      "debug": "node --inspect-brk" 
    } 
  }

after, run use command: npm run debug yourFile.js

or you can execute in oneline node --inspect-brk yourFile.js

after this: open chrome and go chrome://inspect

wait for a few seconds, to appears a remote target and click inspect

enter image description here

you can use debbuger; in your code to make a breakpoint.

To learn more go to the documentation.

In alternative you can use a plugin for VS code 'Debugger for Chrome' and follow the guide.

Aymen
  • 1,141
  • 12
  • 22