Facebook’s Yarn vs npm — Is Yarn really better?

Nikhil John
Oct 12, 2016 · 4 min read

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?

Yarn … cat?

This article is from Oct 2016. For a more recent comparison, checkout my post on npm 5 vs Yarn

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 your terminal. You can install it using Bash or npm or any other methods listed here.

curl -o- -L https://yarnpkg.com/install.sh | bash

or

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

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 lodash@1.0.2: 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.

Speed

One of Yarn’s most exciting features is its speed.

Yarn parallelizes operations to maximize resource utilization so install times are faster than ever
— yarnpkg.com

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 vs 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 a slick tool, that saves developers valuable time and effort. Go ahead and try it in your codebase and leave your comments and feedback!

Happy coding!

This post was featured on Scotch.io

Zero Equals False

Zero Equals False delivers quality content to the Software community.

By Zero Equals False

Zero Equals False - delivering quality content to the Software community. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Zero Equals False

Zero Equals False delivers quality content to the Software community. For more quality content, head over to https://zeroequalsfalse.com

Nikhil John

Written by

Tips and tricks on Travelling and Web Engineering because I’m a Travelling Web Engineer!

Zero Equals False

Zero Equals False delivers quality content to the Software community. For more quality content, head over to https://zeroequalsfalse.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store