r/javascript • u/cht8687 • Jan 26 '16
You-Dont-Need-Lodash-Underscore
https://github.com/cht8687/You-Dont-Need-Lodash-Underscore5
u/arcanin ๐งถ Yarn maintainer Jan 26 '16
I don't understand why so many devs use the Lodash versions of native functions instead of adding a shim. You can't both complain about a lack of native tools and not use the ones we have, folks.
To be clear, I have absolutely no issue with Lodash in itself - I use it a lot for the new tools it brings to the language. My remark only applies on those who are natively available.
4
u/pimterry Jan 26 '16
If you're using lots of lodash functions in addition to the native ones, it's cleaner to read and write code running everything through there, rather than special-casing the ones that are worth shimming.
Internally lodash is pretty good at falling back to native functions where available if it's sensible, remaining extremely compatible with them (so you could migrate very easily if you wanted), and ensuring great performance.
Personally I use lots and lots of lodash functions, and while the ones listed here are useful to have natively, they're not the majority of the methods I use. Lodash is really good, native has a long way to include everything Lodash has (and I'm not sure it would be sensible to do so), and if you're going to include Lodash anyway you might as well use it for everything.
4
u/teapoted Jan 26 '16
Lodash performs better than Native for a lot of it's implementations.
The primary reason is for consistency though, especially if you're using a more functional style with a lot of currying and composing.
3
u/arcanin ๐งถ Yarn maintainer Jan 26 '16
See, I don't really see performances as a valid reason to use these functions. I have yet to see an application where
Array#forEachwas the real bottleneck, and if you're writing a library, I'd very much prefer the library author to avoid dependencies, even if it means they have to implement a basic forEach that solves their particular use case, than requiring me to use Underscore or Lodash or whatever. I love taking libraries for a date, but I hate it when they require me to bring their friends along.As for the consistency, it might make sense when making use of the functional style (since Lodash brings new features in this regard), but not quite as much when writing imperative code - one might argue that using the standard library is the actual best way to achieve consistency.
3
u/teapoted Jan 26 '16
As I said, the primary reason isn't really about performance.
Getting more consistency from using the standard library is arguable. Consistency comes down to programming style and the overall composition of your program. It's not consistent if in your code you are using some of the native array methods but then have to constantly break out to write some for loop to do something those methods won't do. Or if you have to operate on objects similarly.
2
u/NeuroXc Jan 26 '16
As has been said numerous times, Lodash provides more consistency (between methods and across browsers) and improves support for objects and convenience calls when dealing with collections (arrays of objects).
Why use a shim when you can use a fairly lightweight library that does the same thing better?
8
Jan 26 '16
Native implematations are simply faster
In all browsers? What tests are you basing this on?
9
Jan 26 '16
This is blatantly not true in the case of forEach. Native is substantially slower.
3
Jan 26 '16 edited Jan 27 '16
1
0
Jan 26 '16
Thanks bud. On mobile/lazy.
2
u/spiralxuk Jan 26 '16
It doesn't really support your position though, Lodash's
eachmethod isn't that much faster than native, andforEachis faster than all of the other libraries implementations. If you're calling it enough for that difference to matter, you should probably be hand-rolling a plainforloop anyway.2
Jan 26 '16
In practice, you're absolutely right. I was just refuting a claim that has since been removed. Overall, I agree with this write up, just not that specific claim.
I was very curious as to why that is when I found it out, this SO answer sums it up nicely.
4
u/bytasv Jan 26 '16
Things that are shown in the repo are just tip of an iceberg why one would want to use lodash or underscore.
4
u/bdchauvette Jan 26 '16
Since v3, lodash also uses lazy evaluation for chaining, and even offers shortcut fusion for some methods. In many cases, this can provide a huge performance improvement over native methods.
(I'm not sure if underscore has lazy evaluation.)
6
u/xxxabc123 Jan 26 '16
Not very convincing if you say some methods are completely unsupported in IE or Opera. Besides underscore itself is only 5.7 kb, at least you-dont-need-jQuery posts made sense for the size aspect.
2
u/atmin_ Jan 26 '16
All examples currently not supported by IE or Opera can be reimplemented using .filter(), which won't add significant amount of code.
1
u/AceBacker Jan 26 '16
Most of the tools I've needed have been in jQuery.
Is the tooling in Underscore better than jQuery?
1
u/MaoStevemao Jan 29 '16
In response to you, I have created https://github.com/stevemao/You-Dont-Know-About-Lodash-Underscore
1
1
0
15
u/AngryParsley Jan 26 '16 edited Jan 26 '16
Most of these examples use arrays and ignore objects. That causes the post to overlook quite a few advantages of underscore and lodash. For example,
_.each()can take an object instead of an array. The first arg passed to the function is the value. The second is the key. Also, in lodash, returningfalsewill end iteration early. It's likebreakin loops. These may seem like minor differences, but they allow for more flexible, powerful, and straightforward code.And that's just one function. There are lots of similar nice-to-haves that make lodash and underscore worthwhile. Heck, I even use lodash for server-side stuff.
Edit: Fixed grammar.