BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Lodash, the JavaScript Library You're Already Using

Lodash, the JavaScript Library You're Already Using

This item in japanese

Lire ce contenu en français

Bookmarks

The JavaScript utility library, lodash, reached version 3.5 and became the most depended on library in the npm package repository, shaking off its underdog status and becoming the go-to choice. 

It started out as a fork of the Underscore.js library; the result of a difference of opinion among contributors. The original goal, led by John-David Dalton, was to provide more "consistent cross-browser behavior ... and improved performance". Since then, the project has continued to build on its success and in January, version 3.0 was released.

Lodash, like Underscore before it, is named for the single character that all functionality hangs from. Much like jQuery, which attaches everything to a global $, lodash uses a global _ to offer quick access to useful tools. For example, to perform an action on every element of an array:

_.each([1, 2], function(n) { console.log(n); });

Version 3 came with a lot of changes such as the addition of 47 new methods like _.camelCase and _.flattenDeep. More significant is the switch to lazy evaluation for chained methods which let developers connect calls to run in sequence:

var users = [
  { 'user': 'barney',  'age': 36 },
  { 'user': 'fred',    'age': 40 },
  { 'user': 'pebbles', 'age': 1 }
];

var youngest = _.chain(users)
  .sortBy('age')
  .map(function(chr) {
    return chr.user + ' is ' + chr.age;
  })
  .first()
  .value();
// → 'pebbles is 1'

In an interview with InfoQ, Dalton said that using lazy evaluation on these methods allows lodash to optimize performance:

Lazy evaluation means that the chain of methods isn't executed until an implicit or explicit value() is required. Because execution is deferred, lodash can apply optimizations like shortcut fusion which can greatly reduce the number of iterations by merging chained iteratees.

Filip Zawada describes how the change can result in a major performance boost.

Originally, lodash started out as a drop-in replacement for Underscore, but as of version 3.0, there is no longer a build specific to Underscore. "While we still test against Underscore/Backbone unit tests, with lodash v3 we no longer support a separate Underscore/Backbone build," said Dalton.

Over the last year we’ve seen Underscore align more & more with lodash’s API so the need for a separate Underscore build has diminished. If developers still need compatibility around some of the edges we recommend leveraging modules in lodash v3 to supplement their Underscore use.

Some major npm packages depend on lodash such as the JavaScript transpiler Babel, the blogging platform Ghost, and the project scaffolding tool Yeoman. In the case of Ghost, the project originally used Underscore but switched to lodash. Asked by InfoQ about the inclusion of lodash in Ghost, John O'Nolan, its founder said, "It was a very pragmatic decision that was fueled pretty much entirely by our open source development community."

We found lodash to have more features, better performance, uses semver appropriately, and is becoming extremely prominent in the Node.js community (including throughout our other dependencies).

Though lodash is popular with node.js developers, it is also used by browser-based projects. Web developers can use the experimental build tool to cherry-pick which methods their project needs rather than downloading the entire library. For node.js projects, individual methods can be included when the whole package isn't needed.

Rate this Article

Adoption
Style

BT