Lodash and Underscore are great modern JavaScript utility libraries, and they are widely used by Front-end developers. However, when you are targeting modern browsers, you may find out that there are many methods which are already supported natively thanks to ECMAScript5 [ES5] and ECMAScript2015 [ES6]. If you want your project to require fewer dependencies, and you know your target browser clearly, then you may not need Lodash/Underscore.
You are welcome to contribute with more items provided below.
If you are targeting legacy JavaScript engine with those ES5 methods, you can use es5-shim
Please note that, the examples used below are just showing you the native alternative of performing certain tasks. For some functions, Lodash provides you more options than native built-ins. This list is not a 1:1 comparison.
Please send a PR if you want to add or modify the code. No need to open an issue unless it's something big and you want to discuss.
Make use of native JavaScript object and array utilities before going big.
—Cody Lindley, Author of jQuery Cookbook and JavaScript Enlightenment
You probably don't need Lodash. Nice List of JavaScript methods which you can use natively.
—Daniel Lamb, Computer Scientist, Technical Reviewer of Secrets of the JavaScript Ninja and Functional Programming in JavaScript
—Tero Parviainen, Author of build-your-own-angular
I'll admit, I've been guilty of overusing #lodash. Excellent resource.
—@therebelrobot, Maker of web things, Facilitator for Node.js/io.js
If you're using ESLint, you can install a plugin that will help you identify places in your codebase where you don't (may not) need Lodash/Underscore.
Install the plugin ...
npm install --save-dev eslint-plugin-you-dont-need-lodash-underscore
... then update your config
"extends" : ["plugin:you-dont-need-lodash-underscore/compatible"],
For more information, see Configuring the ESLint Plugin
[!IMPORTANT] Note that, while many Lodash methods are null safe (e.g. .keys, .entries), this is not necessarily the case for their Native equivalent. If null safety is critical for your application, we suggest that you take extra precautions [e.g. consider using the native
Object.keysasObject.keys(value || {})].
[!IMPORTANT] Note that most native equivalents are array methods, and will not work with objects. If this functionality is needed and no object method is provided, then Lodash/Underscore might be the better option. Bear in mind however, that all iterable objects can easily be converted to an array by use of the spread operator.
Creates an array of elements split into groups the length of size.
// Underscore/Lodash
_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]
_.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]
// Native
const chunk = (input, size) => {
return input.reduce((arr, item, idx) => {
return idx % size === 0
? [...arr, [item]]
: [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
}, []);
};
chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]
chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]
![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | 46.0 ✔ | 12.0 ✔ | 16.0 ✔ | ✖ | 37.0 ✔ | 8.0 ✔ |
Creates an array with all falsy values removed.
```js // Underscore/Lodash _.compact([0, 1, false, 2, '', 3]); // output: [1, 2, 3]
// Native [0, 1, false, 2, '', 3].filter(Boolean) // output: [1, 2, 3] ```
array.prototype.filter![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | ✔ | ✔ | 1.5 ✔ | 9.0 ✔ | ✔ | ✔ |
Creates a new array concatenating array with any additional arrays and/or values.
```js // Underscore/Lodash var array = [1] var other = _.concat(array, 2, [3], [[4]])
console.log(other) // output: [1, 2, 3, [4]]
// Native var array = [1] var other = array.concat(2, [3], [[4]])
console.log(other) // output: [1, 2, 3, [4]] ```
Array.prototype.concat()![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | 1.0 ✔ | ✔ | 1.0 ✔ | 5.5 ✔ | ✔ | ✔ |
Similar to without, but returns the values from array that are not present in the other arrays.
```js // Underscore/Lodash console.log(_.difference([1, 2, 3, 4, 5], [5, 2, 10])) // output: [1, 3, 4]
// Native var arrays = [[1, 2, 3, 4, 5], [5, 2, 10]]; console.log(arrays.reduce(function(a, b) { return a.filter(function(value) { return !b.includes(value); }); })); // output: [1, 3, 4]
// ES6 let arrays = [[1, 2, 3, 4, 5], [5, 2, 10]]; console.log(arrays.reduce((a, b) => a.filter(c => !b.includes(c)))); // output: [1, 3, 4] ```
Array.prototype.reduce()![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | ✔ | ✔ | 3.0 ✔ | 9.0 ✔ | 10.5 ✔ | 4.0 ✔ |
Creates a slice of array with n elements dropped from the beginning.
```js // Underscore/Lodash _.drop([1, 2, 3]); // => [2, 3]
_.drop([1, 2, 3], 2); // => [3]
// Native [1, 2, 3].slice(1); // => [2, 3]
[1, 2, 3].slice(2); // => [3] ```
Array.prototype.slice()![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | 1.0 ✔ | ✔ | 1.0 ✔ | ✔ | ✔ | ✔ |
Creates a slice of array with n elements dropped at the end.
```js // Underscore/Lodash _.dropRight([1, 2, 3]); // => [1, 2]
_.dropRight([1, 2, 3], 2); // => [1]
// Native [1, 2, 3].slice(0, -1); // => [1, 2]
[1, 2, 3].slice(0, -2); // => [1] ```
Array.prototype.slice()![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | 1.0 ✔ | ✔ | 1.0 ✔ | ✔ | ✔ | ✔ |
Fills elements of array with value from start up to, but not including, end.
[!NOTE]
fillis a mutable method in both native and Lodash/Underscore.
```js // Underscore/Lodash var array = [1, 2, 3]
_.fill(array, 'a')
console.log(array) // output: ['a', 'a', 'a']
_.fill(Array(3), 2) // output: [2, 2, 2]
_.fill([4, 6, 8, 10], '', 1, 3) // output: [4, '', '*', 10]
// Native var array = [1, 2, 3]
array.fill('a')
console.log(array) // output: ['a', 'a', 'a']
Array(3).fill(2) // output: [2, 2, 2]
[4, 6, 8, 10].fill('', 1, 3) // output: [4, '', '*', 10] ```
Array.prototype.fill()![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | 45.0 ✔ | ✔ | 31.0 ✔ | ✖ | 32.0 ✔ | 8 ✔ |
Returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
```js // Underscore/Lodash var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]
_.find(users, function (o) { return o.age < 40; }) // output: object for 'barney'
// Native var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]
users.find(function (o) { return o.age < 40; }) // output: object for 'barney' ```
Array.prototype.find()![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] :-: | :-: | :-: | :-: | :-: | :-: | 45.0 ✔ | ✔ | 25.0 ✔ | ✖ | 32.0 ✔ | 7.1 ✔ |
Returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.
```js // Underscore/Lodash var users = [ { 'user': 'barney',
$ claude mcp add You-Dont-Need-Lodash-Underscore \
-- python -m otcore.mcp_server <graph>