Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the differences between lodash and underscore?
Lodash and Underscore are both utility libraries that make JavaScript easier by providing utils that make working with arrays, numbers, objects, strings, etc. much easier. These libraries are great for:
Iterating arrays, objects, & strings
Manipulating & testing values
Creating composite functions
They are both functional libraries. Lodash is a fork of Underscore, and still follows Underscore's API enough to allow it to serve as a drop-in replacement. But under the hood, it's been completely rewritten, and it's also added a number of features and functions that Underscore does not provide.
History and Relationship
Lodash was created to provide more consistent cross-environment iteration support for arrays, strings, objects, and argument objects. It has since become a superset of Underscore providing extra features (like AMD support, deep clone, and deep merge), better overall performance and optimizations for large arrays/object iteration, and more flexibility with custom builds and template pre-compilation utilities.
Key Differences
Performance
Lodash generally outperforms Underscore, especially with large datasets:
// Example: Array iteration performance
const largeArray = Array.from({length: 10000}, (_, i) => i);
// Lodash _.forEach is typically faster
console.time('Lodash forEach');
_.forEach(largeArray, (item) => {
// Process item
});
console.timeEnd('Lodash forEach');
Bundle Size and Modularity
Lodash offers better modularity options:
// Underscore - import entire library
const _ = require('underscore');
// Lodash - import specific functions
const map = require('lodash/map');
const filter = require('lodash/filter');
// Or use ES6 imports
import { map, filter } from 'lodash';
Feature Comparison
// Deep cloning - available in Lodash, not in Underscore
const original = { a: { b: { c: 1 } } };
// Lodash has deep clone
const deepCopy = _.cloneDeep(original);
console.log(deepCopy.a.b.c); // 1
// Underscore only has shallow clone
const shallowCopy = _.clone(original);
console.log(shallowCopy === original); // false
console.log(shallowCopy.a === original.a); // true (same reference)
1 false true
Comparison Table
| Feature | Lodash | Underscore |
|---|---|---|
| Performance | Higher | Lower |
| Bundle Size | Larger (but modular) | Smaller |
| Deep Clone | Yes | No |
| AMD Support | Yes | Limited |
| Custom Builds | Yes | No |
| Active Development | Very Active | Minimal |
Common Use Cases
// Both libraries provide similar core functionality
const users = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Bob', age: 35 }
];
// Array manipulation works similarly in both
const names = _.map(users, 'name');
console.log(names); // ['John', 'Jane', 'Bob']
const adults = _.filter(users, user => user.age >= 30);
console.log(adults.length); // 2
['John', 'Jane', 'Bob'] 2
Which Should You Choose?
Choose Lodash if: You need better performance, modern features like deep cloning, or plan to use modular imports to reduce bundle size.
Choose Underscore if: You have a legacy project already using it, or you need a smaller library with just core utilities.
Conclusion
While both libraries serve similar purposes, Lodash has become the more popular choice due to its better performance, additional features, and active development. For new projects, Lodash is generally recommended.
