Lodash is a well-known JavaScript utility library that makes it easy to manipulate arrays and objects, as well as functions, strings, etc. I myself enjoys its functional way to process collections, especially chaining and lazy evaluation. But as ECMAScript 2015 Standard (ES6) becomes widely supported by major browsers, and Babel, the JavaScript compiler that transforms ES6 codes to ES5, plays a major role in today’s frontend development, it seems that most Lodash utilities can be replaced by ES6. But should we? In my opinion, Lodash will remain popular, for it still has lots of useful features that could improve the way of programming.
_.map
and Array#map
Are Different
_.map
, _.reduce
, _.filter
and _.forEach
are frequently used functions when processing collections, and ES6 provides direct support for them:
1 | _.map([1, 2, 3], (i) => i + 1) |
But Lodash’s _.map
is more powerful, in that it works on objects, has iteratee / predicate shorthands, lazy evaluation, guards against null parameter, and has better performance.
Iterate over Objects
To iterate over an object in ES6, there’re several approaches:
1 | for (let key in obj) { console.log(obj[key]) } |
With Lodash, there’s a unified _.forEach
, for both array and object:
1 | _.forEach(obj, (value, key) => { console.log(value) }) |
Although ES6 does provide forEach
for the newly added Map
type, it takes some effort to first convert an object into a Map
:
1 | // http://stackoverflow.com/a/36644532/1030720 |
Iteratee / Predicate Shorthands
To extract some property from an array of objects:
1 | let arr = [{ n: 1 }, { n: 2 }] |
This can be more helpful when it comes to complex objects:
1 | let arr = [ |
As we can see, Lodash not only provides conveniet shorthands, it also guards against undefined values. For _.filter
, there’s also predicate shorthand. Here are some examples from Lodash documentation:
1 | let users = [ |
Chain and Lazy Evaluation
Here comes the fun part. Processing collections with chaining, lazy evaluation, along with short, easy-to-test functions, is quite popular these days. Most Lodash functions regarding collections can be chained easily. The following is a wordcount example:
1 | let lines = ` |
Destructuring, Spread and Arrow Function
ES6 introduces some useful syntaxes like destructuring, spread and arrow function, which can be used to replace a lot of Lodash functions. For instance:
1 | // Lodash |
For collection related operations, I prefer Lodash functions for they are more concise and can be chained; for functions that can be rewritten by arrow function, Lodash still seems more simple and clear. And according to some arguments in the references, the currying, operators and fp style from Lodash are far more useful in scenarios like function composition.
Conclusion
Lodash adds great power to JavaScript language. One can write concise and efficient codes with minor efforts. Besides, Lodash is fully modularized. Though some of its functions will eventually deprecate, but I believe it’ll still bring many benifits to developers, while pushing the development of JS language as well.