Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
eslint-plugin-you-dont-need-lodash-underscore
Advanced tools
Check methods you can use natively without lodash/underscore
The eslint-plugin-you-dont-need-lodash-underscore package is an ESLint plugin that helps developers identify and replace Lodash/Underscore functions with native JavaScript alternatives. This can help reduce bundle size and improve performance by eliminating unnecessary dependencies.
Array Methods
This rule suggests using native JavaScript array methods instead of Lodash/Underscore methods. In this example, it replaces `_.concat` with the native `Array.prototype.concat` method.
/* eslint you-dont-need-lodash-underscore/concat: 'error' */
// Lodash
const result = _.concat([1], 2, [3], [[4]]);
// Native
const result = [1].concat(2, [3], [[4]]);
Object Methods
This rule suggests using native JavaScript object methods instead of Lodash/Underscore methods. In this example, it replaces `_.assign` with the native `Object.assign` method.
/* eslint you-dont-need-lodash-underscore/assign: 'error' */
// Lodash
const result = _.assign({ 'a': 1 }, { 'b': 2 }, { 'c': 3 });
// Native
const result = Object.assign({ 'a': 1 }, { 'b': 2 }, { 'c': 3 });
String Methods
This rule suggests using native JavaScript string methods instead of Lodash/Underscore methods. In this example, it replaces `_.startsWith` with the native `String.prototype.startsWith` method.
/* eslint you-dont-need-lodash-underscore/startsWith: 'error' */
// Lodash
const result = _.startsWith('abc', 'a');
// Native
const result = 'abc'.startsWith('a');
eslint-plugin-lodash is an ESLint plugin that provides rules to enforce best practices when using Lodash. Unlike eslint-plugin-you-dont-need-lodash-underscore, it focuses on optimizing Lodash usage rather than replacing it with native alternatives.
eslint-plugin-compat is an ESLint plugin that helps ensure compatibility with various browsers by warning about the use of unsupported JavaScript features. While it doesn't specifically target Lodash/Underscore, it can help identify when native alternatives are not supported in certain environments.
eslint-plugin-no-use-extend-native is an ESLint plugin that prevents the use of extended native objects. It encourages the use of native methods and can be seen as complementary to eslint-plugin-you-dont-need-lodash-underscore by promoting native JavaScript usage.
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 of the functions, Lodash provides you more options than native built-ins. This list is not a 1:1 comparison.
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
Install the plugin...
npm install --save-dev eslint-plugin-you-dont-need-lodash-underscore
.. then update your config.
'plugins': ['you-dont-need-lodash-underscore'],
'rules': {
'you-dont-need-lodash-underscore/for-each': 1,
'you-dont-need-lodash-underscore/concat': 1,
...
}
:heavy_exclamation_mark:Important: Note that the native equivalents are array methods, and will not work with objects. If this functionality is needed, then Lodash/Underscore is the better option.
Creates a new array concatenating array with any additional arrays and/or values.
// 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]]
1.0 ✔ | 1.0 ✔ | 5.5 ✔ | ✔ | ✔ |
Fills elements of array with value from start up to, but not including, end.
Note that fill
is a mutable method in both native and Lodash/Underscore.
// 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]
45.0 | 31.0 ✔ | Not supported | Not supported | 7.1 |
Returns a value in the array, if an element in the array satisfies the provided testing function. Otherwise undefined is returned.
// 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'
45.0 | 25.0 ✔ | Not supported | Not supported | 7.1 |
Returns an index in the array, if an element in the array satisfies the provided testing function. Otherwise -1 is returned.
// Underscore/Lodash
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
]
var index = _.findIndex(users, function (o) { return o.age >= 40; })
console.log(index)
// output: 1
// Native
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
]
var index = users.findIndex(function (o) { return o.age >= 40; })
console.log(index)
// output: 1
45.0 | 25.0 ✔ | Not supported | Not supported | 7.1 |
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
// Underscore/Lodash
var array = [2, 9, 9]
var result = _.indexOf(array, 2)
console.log(result)
// output: 0
// Native
var array = [2, 9, 9]
var result = array.indexOf(2)
console.log(result)
// output: 0
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
:heavy_exclamation_mark:Lodash only
Joins a list of elements in an array with a given separator.
// Lodash
var result = _.join(['one', 'two', 'three'], '--')
console.log(result)
// output: 'one--two--three'
// Native
var result = ['one', 'two', 'three'].join('--')
console.log(result)
// output: 'one--two--three'
1.0✔ | 1.0✔ | 5.5✔ | ✔ | ✔ |
Returns the index of the last occurrence of value in the array, or -1 if value is not present.
// Underscore/Lodash
var array = [2, 9, 9, 4, 3, 6]
var result = _.lastIndexOf(array, 9)
console.log(result)
// output: 2
// Native
var array = [2, 9, 9, 4, 3, 6]
var result = array.lastIndexOf(9)
console.log(result)
// output: 2
✔ | ✔ | 9 ✔ | ✔ | ✔ |
:heavy_exclamation_mark:Lodash only
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
// Lodash
var array = [1, 2, 3]
console.log(_.reverse(array))
// output: [3, 2, 1]
// Native
var array = [1, 2, 3]
console.log(array.reverse())
// output: [3, 2, 1]
Voice from the Lodash author:
Lodash's
_.reverse
just callsArray#reverse
and enables composition like_.map(arrays, _.reverse).
It's exposed on _ because previously, likeUnderscore
, it was only exposed in the chaining syntax. --- jdalton
1.0✔ | 1.0✔ | 5.5✔ | ✔ | ✔ |
:heavy_exclamation_mark:Important: Note that the native equivalents are array methods, and will not work with objects. If this functionality is needed, then Lodash/Underscore is the better option.
Iterates over a list of elements, yielding each in turn to an iteratee function.
// Underscore/Lodash
_.each([1, 2, 3], function (value, index) {
console.log(value)
})
// output: 1 2 3
// Native
[1, 2, 3].forEach(function (value, index) {
console.log(value)
})
// output: 1 2 3
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
Tests whether all elements in the array pass the test implemented by the provided function.
// Underscore/Lodash
function isLargerThanTen (element, index, array) {
return element >= 10
}
var array = [10, 20, 30]
var result = _.every(array, isLargerThanTen)
console.log(result)
// output: true
// Native
function isLargerThanTen (element, index, array) {
return element >= 10
}
var array = [10, 20, 30]
var result = array.every(isLargerThanTen)
console.log(result)
// output: true
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
Creates a new array with all elements that pass the test implemented by the provided function.
// Underscore/Lodash
function isBigEnough (value) {
return value >= 10
}
var array = [12, 5, 8, 130, 44]
var filtered = _.filter(array, isBigEnough)
console.log(filtered)
// output: [12, 130, 44]
// Native
function isBigEnough (value) {
return value >= 10
}
var array = [12, 5, 8, 130, 44]
var filtered = array.filter(isBigEnough)
console.log(filtered)
// output: [12, 130, 44]
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
Checks if value is in collection.
var array = [1, 2, 3]
// Underscore/Lodash - also called with _.contains
_.includes(array, 1)
// → true
// Native
var array = [1, 2, 3]
array.includes(1)
// → true
47✔ | 43 ✔ | Not supported | 34 | 9 |
Translate all items in an array or object to new array of items.
// Underscore/Lodash
var array1 = [1, 2, 3]
var array2 = _.map(array1, function (value, index) {
return value * 2
})
console.log(array2)
// output: [2, 4, 6]
// Native
var array1 = [1, 2, 3]
var array2 = array1.map(function (value, index) {
return value * 2
})
console.log(array2)
// output: [2, 4, 6]
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
Applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
// Underscore/Lodash
var array = [0, 1, 2, 3, 4]
var result = _.reduce(array, function (previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue
})
console.log(result)
// output: 10
// Native
var array = [0, 1, 2, 3, 4]
var result = array.reduce(function (previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue
})
console.log(result)
// output: 10
✔ | 3.0 ✔ | 9 ✔ | 10.5 | 4.0 |
This method is like _.reduce except that it iterates over elements of collection from right to left.
// Underscore/Lodash
var array = [0, 1, 2, 3, 4]
var result = _.reduceRight(array, function (previousValue, currentValue, currentIndex, array) {
return previousValue - currentValue
})
console.log(result)
// output: -2
// Native
var array = [0, 1, 2, 3, 4]
var result = array.reduceRight(function (previousValue, currentValue, currentIndex, array) {
return previousValue - currentValue
})
console.log(result)
// output: -2
✔ | 3.0 ✔ | 9 ✔ | 10.5 | 4.0 |
Return the number of values in the collection.
// Underscore/Lodash
var result = _.size({one: 1, two: 2, three: 3})
console.log(result)
// output: 3
// Native
var result2 = Object.keys({one: 1, two: 2, three: 3}).length
console.log(result2)
// output: 3
5✔ | 4.0 ✔ | 9 | 12 | 5 |
Tests whether some element in the array passes the test implemented by the provided function.
// Underscore/Lodash
function isLargerThanTen (element, index, array) {
return element >= 10
}
var array = [10, 9, 8]
var result = _.some(array, isLargerThanTen)
console.log(result)
// output: true
// Native
function isLargerThanTen (element, index, array) {
return element >= 10
}
var array = [10, 9, 8]
var result = array.some(isLargerThanTen)
console.log(result)
// output: true
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
:heavy_exclamation_mark:Note this is an alternative implementation
Creates a version of the function that will only be run after first being called count times. Useful for grouping asynchronous responses, where you want to be sure that all the async calls have finished, before proceeding.
var notes = ['profile', 'settings']
// Underscore/Lodash
var renderNotes = _.after(notes.length, render)
notes.forEach(function (note) {
console.log(note)
renderNotes()
})
// Native
notes.forEach(function (note, index) {
console.log(note)
if (notes.length === (index + 1)) {
render()
}
})
✔ | ✔ | ✔ | ✔ | ✔ |
Checks if value is NaN.
// Underscore/Lodash
console.log(_.isNaN(NaN))
// output: true
// Native
console.log(isNaN(NaN))
// output: true
// ES6
console.log(Number.isNaN(NaN))
// output: true
MDN:
In comparison to the global
isNaN()
function,Number.isNaN()
doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert toNaN
, but aren't actually the same value asNaN
. This also means that only values of the type number, that are alsoNaN
, return true. Number.isNaN()
Voice from the Lodash author:
Lodash's
_.isNaN
is equiv to ES6Number.isNaN
which is different than the globalisNaN
. --- jdalton
isNaN
✔ | ✔ | ✔ | ✔ | ✔ |
Number.isNaN
25 | 15 | Not supported | ✔ | 9 |
The method is used to copy the values of all enumerable own properties from one or more source objects to a target object.
// Underscore: _.extendOwn
// Lodash
function Foo() {
this.c = 3;
}
function Bar() {
this.e = 5;
}
Foo.prototype.d = 4;
Bar.prototype.f = 6;
var result = _.assign(new Foo, new Bar);
console.log(result);
// output: { 'c': 3, 'e': 5 }
// Native
function Foo() {
this.c = 3;
}
function Bar() {
this.e = 5;
}
Foo.prototype.d = 4;
Bar.prototype.f = 6;
var result = Object.assign(new Foo, new Bar);
console.log(result);
// output: { 'c': 3, 'e': 5 }
45✔ | 34✔ | No support | 32✔ | 9✔ |
Retrieve all the names of the object's own enumerable properties.
// Underscore/Lodash
var result = _.keys({one: 1, two: 2, three: 3})
console.log(result)
// output: ["one", "two", "three"]
// Native
var result2 = Object.keys({one: 1, two: 2, three: 3})
console.log(result2)
// output: ["one", "two", "three"]
5✔ | 4.0 ✔ | 9 | 12 | 5 |
:heavy_exclamation_mark:Lodash only
Repeats the given string n times.
// Lodash
var result = _.repeat('abc', 2)
// output: 'abcabc'
// Native
var result = 'abc'.repeat(2)
console.log(result)
// output: 'abcabc'
41✔ | 24✔ | Not supported | Not supported | 9 |
:heavy_exclamation_mark:Lodash only
Lowercase a given string.
// Lodash
var result = _.toLower('FOOBAR')
console.log(result)
// output: 'foobar'
// Native
var result = 'FOOBAR'.toLowerCase()
console.log(result)
// output: 'foobar'
✔ | ✔ | ✔ | ✔ | ✔ |
:heavy_exclamation_mark:Lodash only
Uppercase a given string.
// Lodash
var result = _.toUpper('foobar')
console.log(result)
// output: 'FOOBAR'
// Native
var result = 'foobar'.toUpperCase()
console.log(result)
// output: 'FOOBAR'
✔ | ✔ | ✔ | ✔ | ✔ |
:heavy_exclamation_mark:Lodash only
Removes leading and trailing whitespace characters from string.
// Lodash
var result = _.trim(' abc ')
console.log(result)
// output: 'abc'
// Native
var result = ' abc '.trim()
console.log(result)
// output: 'abc'
5.0✔ | 3.5✔ | 9.0✔ | 10.5✔ | 5.0✔ |
MIT
FAQs
Check methods you can use natively without lodash/underscore
The npm package eslint-plugin-you-dont-need-lodash-underscore receives a total of 120,674 weekly downloads. As such, eslint-plugin-you-dont-need-lodash-underscore popularity was classified as popular.
We found that eslint-plugin-you-dont-need-lodash-underscore demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.