Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm run <cmd> works slow

I used to run various commands via Makefile but for nodejs projects package.json is a more appropriate place for this stuff.

Running commands via npm works great but very slow comparing with a command time execution.

$ time ./node_modules/.bin/jshint . && ./node_modules/.bin/jscs .

real    0m0.759s
user    0m0.524s
sys 0m0.085s
No code style errors found.

$ time npm run lint

> @ lint /path/to/project
> jshint . && jscs .

No code style errors found.

real    0m2.246s
user    0m1.637s
sys 0m0.277s

It is possible to speed it up?

upd. My package.json:

{
  "devDependencies": {
    "jscs": "^1.12.0",
    "jshint": "^2.6.3"
  },
  "scripts": {
    "lint": "jshint . && jscs ."
  }
}

upd2. I measured time in the wrong way. Gant has pointed to it in his comment. Now both times look similar (100ms difference).

$ time sh -c './node_modules/.bin/jshint . && ./node_modules/.bin/jscs .'
No code style errors found.

real    0m1.704s
user    0m1.245s
sys 0m0.177s
$ time npm run lint

> @ lint /path/to/project
> jshint . && jscs .

No code style errors found.

real    0m1.822s
user    0m1.621s
sys 0m0.198s
like image 921
Alexander Avatar asked Apr 15 '15 21:04

Alexander


1 Answers

This isn't npm's fault, in fact it even measures the time more accurately.

Let's look at what you did.

time npm run lint will invoke time, which will then invoke your tasks in order. time will stop taking the time needed when npm has quit, which it will when your tasks are done running. There is only slight overhead here and the script is working as intended.

time ./node_modules/.bin/jshint . && ./node_modules/.bin/jscs . however will invoke time, which will invoke jshint. Once jshint quits, time will quit too and jscs will be run without. This is also why in your examples the order of outputs is mixed up.

To get accurate time measurements without npm, try time sh -c './node_modules/.bin/jshint . && ./node_modules/.bin/jscs .'

like image 60
Aurelia Avatar answered Oct 06 '22 01:10

Aurelia