cli icon indicating copy to clipboard operation
cli copied to clipboard

small optimization

Open sirenkovladd opened this issue 1 year ago • 1 comments

What

  1. Reduced barrel issue
  2. ~small optimization with process.title~
  3. moved the optional require

Describe the request in detail. What it does and why it's being changed.

  1. Barrel issue is known for importing files that you don't really need it takes extra time when running any code (this reduced execution time by ~15%)
const { cleanUrl } = require('npm-registry-fetch')
// ->
const cleanUrl = require('npm-registry-fetch/lib/clean-url')
  1. ~changing process.title is a synchronous operation that blocks the thread, it is better to reduce the number of calls of this command (allowed to reduce the execution time by 5%)~

  2. moved the optional require, imports only what is used, ./utils/replace-info.js is quite a heavy dependency, moving it to the function where it is called allowed to reduce the time by 5%

Result:

❯ hyperfine -w 5 -r 20 'npm run echo' 'node bin/npm-cli.js run echo'
Benchmark 1: npm run echo
  Time (mean ± σ):     256.4 ms ±   5.2 ms    [User: 253.4 ms, System: 34.6 ms]
  Range (min … max):   251.4 ms … 273.2 ms    20 runs
 
Benchmark 2: node bin/npm-cli.js run echo
  Time (mean ± σ):     201.5 ms ±   5.0 ms    [User: 183.7 ms, System: 24.1 ms]
  Range (min … max):   197.7 ms … 221.4 ms    20 runs
 
Summary
  node bin/npm-cli.js run echo ran
    1.27 ± 0.04 times faster than npm run echo

sirenkovladd avatar Jan 24 '24 06:01 sirenkovladd

❯ hyperfine -w 5 -r 20 'npm run echo' 'node bin/npm-cli.js run echo'
Benchmark 1: npm run echo
  Time (mean ± σ):     253.7 ms ±   4.0 ms    [User: 252.9 ms, System: 34.0 ms]
  Range (min … max):   250.2 ms … 268.7 ms    20 runs
 
Benchmark 2: node bin/npm-cli.js run echo
  Time (mean ± σ):     208.4 ms ±   5.0 ms    [User: 184.4 ms, System: 24.1 ms]
  Range (min … max):   205.9 ms … 229.5 ms    20 runs
 
  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
 
Summary
  node bin/npm-cli.js run echo ran
    1.22 ± 0.04 times faster than npm run echo

sirenkovladd avatar Jan 24 '24 21:01 sirenkovladd

This has been addressed by https://github.com/npm/cli/pull/7334

wraithgar avatar Apr 09 '24 20:04 wraithgar