r/javascript • • Jan 26 '16

You-Dont-Need-Lodash-Underscore

https://github.com/cht8687/You-Dont-Need-Lodash-Underscore
0 Upvotes

30 comments sorted by

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, returning false will end iteration early. It's like break in 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.

1

u/OKC_js Jan 27 '16

For a deep dive on exactly why non-native methods could be faster, and why this post is ignoring these kinds of cases:

John-David Dalton - Unorthodox Performance -ThunderPlains 2015

See for yourself. This is almost a case-by-case literal and code-centric explanation as to why noone should take this repo at face value.

1

u/Doctuh Jan 26 '16

And you can't forget the power of _.chain

0

u/atmin_ Jan 26 '16

There's e.g. Object.keys(obj).forEach(...) and similar. _ is useful, but not always needed, especially if you care how many dependencies your project has. For a functional construct-heavy project, I'd choose Ramda.

1

u/i_have_a_gub Jan 26 '16

I'll always choose Ramda. I find it much more intuitive and I feel like having data passed as the last parameter to functions allows for much more composable, clean code.

2

u/[deleted] Jan 26 '16

Lodash v4 has fp included for immutable auto-curried iteratee-first data-last methods

var _ = require('lodash/fp');
_.map(parseInt)(['6', '08', '10'])

2

u/i_have_a_gub Jan 26 '16

Yeah, but I feel more like I'm one of the cool kids when I use Ramda.

3

u/[deleted] Jan 31 '16 edited Jan 31 '16

With lodash/fp you can be a cool kid whose apps don't hit critical perf issues or have to be rewrite for every minor upgrade. It's also cool to not have to wonder about support because fp is tested in browsers & node ๐Ÿ˜Ž

5

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#forEach was 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

u/[deleted] Jan 26 '16

Native implematations are simply faster

In all browsers? What tests are you basing this on?

9

u/[deleted] Jan 26 '16

This is blatantly not true in the case of forEach. Native is substantially slower.

3

u/[deleted] Jan 26 '16 edited Jan 27 '16

1

u/[deleted] Jan 26 '16

Your link gives: 404 Not Found

0

u/[deleted] Jan 26 '16

Thanks bud. On mobile/lazy.

2

u/spiralxuk Jan 26 '16

It doesn't really support your position though, Lodash's each method isn't that much faster than native, and forEach is 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 plain for loop anyway.

2

u/[deleted] 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/9thHokageHimawari Jan 26 '16

OP obviously never used them.

1

u/devvie Jan 26 '16

Chaining. 'nuff said.

0

u/[deleted] Jan 26 '16

[deleted]

0

u/cht8687 Jan 26 '16

Hi, I've changed some word to make it less aggressive...