Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: Running npm on Windows runs node executable twice (which is slow), to find the prefix #3088

Closed
jakub-g opened this issue Apr 15, 2021 · 1 comment
Labels
Bug thing that needs fixing platform:windows is Windows-specific Priority 2 secondary priority issue Release 7.x work is associated with a specific npm 7 release Release 8.x work is associated with a specific npm 8 release

Comments

@jakub-g
Copy link
Contributor

jakub-g commented Apr 15, 2021

I'm not sure if this is feasible / easy to optimize, but when profiling some of our scripts, I noticed the following:

As a Windows user, when I run npm through git bash in my shell [1], or have a JS script which uses child_process.spawn() which calls npm run something, I expect that node executable will be run only once as a result of such command.

However, invoking npm in both cases (either through Windows .cmd or git bash compatible .sh) runs npm prefix -g internally.

So every time any npm command is run, the penalty of booting up a node.exe executable happens twice (on my Dell machine it's about 600-700ms).

It's not a huge issue in itself, but when relying on scripts in package.json, especially when those scripts are compounded (i.e. npm run x calls npm run y), this penalty of multiple node executable calls accumulates fast.

Current Behavior:

Running npm through terminal or spawn runs node.exe twice, incurring the penalty of node.exe boot twice

Expected Behavior:

Running npm should ideally run node.exe only once.

Or at least, in case of nested invocations, there could be perhaps some optimization: 2nd and further calls to npm could reuse an env var exported by the first, top-level npm invocation? (i.e. avoid calling npm prefix -g more than once per lifetime of a top-level script)

(Although I know it can be tricky to rely on the env var, because some script may change it at runtime and break things).

Steps To Reproduce:

  1. Create a file C:\git\node_debug\index.js with following contents:
require('fs').writeFileSync(`${__dirname}/tmp_${Date.now()}`, `${process.argv.join(' ')}\r\n`)
  1. Run npm through git bash using NODE_OPTIONS to run the hook above on each node.exe creation:
NODE_OPTIONS='--require C:\git\node_debug\index.js' npm
  1. Observe that two temp files were written to disk in C:\git\node_debug

Environment:

Windows 10 (git bash), node@12.22.1, npm@6.14.12 (but behavior will be same with npm@7.9.0 as it calls npm prefix -g too)

@jakub-g jakub-g added Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Apr 15, 2021
@nlf nlf added Priority 2 secondary priority issue and removed Needs Triage needs review for next steps labels Apr 16, 2021
@ruyadorno ruyadorno added Release 8.x work is associated with a specific npm 8 release platform:windows is Windows-specific labels Apr 4, 2022
@milaninfy
Copy link
Contributor

Closing: This is fixed by #7271

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing platform:windows is Windows-specific Priority 2 secondary priority issue Release 7.x work is associated with a specific npm 7 release Release 8.x work is associated with a specific npm 8 release
Projects
None yet
Development

No branches or pull requests

4 participants