In case you haven’t heard, Facebook just open-sourced Yarn, its own dependency manager. And it’s fast! But how good is it really?
Say Hi to Yarn, that claims to best npm at Dependency Management. Yarn is super-simple to install and use. It installs over npm, and replaces npm in you terminal. You can install it using Bash or npm or any other methods listed here.
curl -o- -L https://yarnpkg.com/install.sh | bash
npm install --global yarn
Usage is quite similar to npm
yarn # Install all dependencies from package.json yarn install # Alias for yarn yarn init # Initialize an npm repository yarn add [package] # Install npm package yarn upgrade [package] # Upgrade npm package yarn remove [package] # Uninstall npm package
Yet another dependency manager? Why use Yarn?
According to Facebook's announcement, the immediate need for Yarn was the npm’s dependency on having an active internet connection, which broke down Continuous Integration on their offline Sandbox environments i.e. npm install doesn’t work if your Environment is offline.
This means that you can be offline, but still install your npm packages using Yarn if you have installed them at some point in the past.
Let's give this a shot. I am running all commands in an ExpressJS respository that I generated using the Express Generator.
npm install express-generator -g express myapp npm install
If we remove our
node_modules and try to run an
npm install, we see that npm throws an error
$ rm -rf node_modules $ npm install : npm ERR! code ENOTFOUND npm ERR! errno ENOTFOUND :
Now let's try the same thing with Yarn
$ rm -rf node_modules $ yarn install yarn install v0.15.1 [1/4]Resolving packages... [2/4]Fetching packages... warning email@example.com: The engine "rhino" appears to be invalid. [3/4]Linking dependencies... [4/4]Building fresh packages... success Saved lockfile.
Voila! All packages installed! This works because Yarn pulls the packages from it's global cache, where it stores every package it ever downloads.
One of Yarn's most exciting features is it's speed.
Yarn parallelizes operations to maximize resource utilization so install times are faster than ever
This calls for a comparison of both tools on a production repository.
The benchmarking methodology that I followed, is given below. The process followed was the same for npm and yarn. I am using the same ExpressJS repository for both tests.
rm -rf node_modules # remove installed dependencies npm cache clean # clean cache time npm i # install packages : : npm i 29.49s user 8.02s system 19% cpu 3:13.15 total
rm -rf node_modules yarn.lock # remove installed dependencies, lock file npm cache clean # clean cache time npm i # install packages time yarn yarn install v0.15.1 info No lockfile found. [1/4] Resolving packages... : [2/4] Fetching packages... : [3/4] Linking dependencies... [4/4] Building fresh packages... success Saved lockfile. Done in 37.98s. yarn 14.06s user 7.01s system 55% cpu 38.011 total
The results are quite unambiguous. Yarn proves to be consistently faster than npm.
Yarn is on an average, 4.7 times faster than npm
Now imagine what this could do to production build times for gigantic projects!
Other awesome features of Yarn
- Yarn uses checksums to verify the integrity of every installed package before executing code.
- Concise lockfile format, and a deterministic algorithm for installs. This means that Yarn is able to guarantee that an install that worked on one system will work exactly the same way on any other system. Isn’t that what you always wanted?
- npm and bower — Install any package from either npm or Bower and keep your package workflow the same.
- Flat mode — Resolve mismatching versions of dependencies to a single version to avoid creating duplicates.
- Network Resilience — A single request failing won’t cause an install to fail. Requests are retried upon failure.
Overall, Yarn seems to be quite slick, that saves developers valuable time and effort. Go ahead and try it in your codebase and leave your comments and feedback!