segunda-feira, 24 de outubro de 2016

Benchmark npm vs yarn

Few weeks ago Yarn was released to node community, it is an alternative solution to npm and others.

I heard a co-worked saying it was faster than npm, I found it interesting and I decided to do a quick benchmark using both npm and yarn with the installation of the project I work on.

I've done three tests:
  • Installation with yarn
  • Installation with npm
  • Installation with npm with cache

Yarn gets data from npm repository. When running yarn install, it uses cache by default, differently from npm, which you have to include an option to the install command, example: npm --cache-min 9999999 install

I've used the lib rmdir to clean up the directory node_modules before each execution. And I've also used child_process.exec to run the install command.
Note, rmdir uses child_process.exec behind the scenes. And rmdir could return a promise instead of requiring a callback :P.

Here is the code I ran to do the benchmark:

var rmdir = require('rmdir'),
    exec = require('child_process').exec;

rmAndInstall('yarn');
rmAndInstall('npm');
rmAndInstall('npm', '--cache-min 9999999');

function rmAndInstall(libName, options) {
    rmdir("node_modules", () => {
        const init = new Date();
        const opts = options === undefined ? '' : options
        console.log('Executing '+libName+' ' +opts+' install');
        exec(libName+' ' +opts+' install', () => {
            var result = (new Date() - init).toString();
            console.log(libName+' '+opts+' took: '+ result);
        });
    });
}

And here are the results in milliseconds, interesting humm?!

mcure@cure:~/github/project$ node app/benchmark_npm_yarn.js 
installing stuff with yarn
installing stuff with npm
installing stuff with npm --cache-min 9999999
yarn took: 8930
npm took: 35383
npm --cache-min 9999999 took: 28226

And that's really impressive because yarn execution time was almost 4x faster than npm using cache.

To be honest, I don't know exactly how mature yarn is and neither about its bugs, but I'll definitely push my team to use yarn, at least for evaluating it. Also I want to try all features and learn more about the tool.

It was a very simple post, but I hope I helped you to know a bit more about yarn. And I hope you get encouraged to try it as well.

The benchmark code is also on my github.

Tweet me if you wanna discuss more about it.