Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation suggestion: why is prefer-lodash-method recommended? #98

Closed
danvk opened this issue Sep 1, 2016 · 1 comment
Closed

Comments

@danvk
Copy link

danvk commented Sep 1, 2016

prefer-lodash-method is included in the lodash/recommended rule set. The docs for that rule just say:

When using native functions like forEach and map, it's often better to use the Lodash implementation.

It would be helpful if this page mentioned the reasons it's "often better" to use the lodash methods. I'm thinking about disabling this warning, but I'd like to know the pros/cons.

@jfmengels
Copy link
Contributor

AFAIK, there are (at least) three reasons:

  • Performance reasons: Lodash methods tend to be quite a bit faster than native ones.
  • It "crashes" less: In the sense that array.map(fn) would crash when array is null, Lodash's methods do not (map returns empty array, forEach does nothing for instance)
  • You can use shorthands that make quite a few iteratees/predicates shorter to write:
const refs = array.map(function(value) {
  return value.ref;
});
const refs = array.map(value => value.ref); // Shorter arrow notation
// vs
const refs = _.map(array, 'ref'); // again, if there are `undefined` values in array, this won't crash either


const bigApple = fruits.find(function(fruit) { // Array.prototype.find exists in ES6
  return fruit.type === 'apple' && fruit.size === 'big';
});
const bigApple = fruits.find(fruit => fruit.type === 'apple' && fruit.size === 'big'); // Shorter arrow notation
// vs
const bigApple = _.find(fruits, {type: 'apple', size: 'big'});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants