Hacker News new | ask | show | jobs
by pmichalina 2930 days ago
lodash and moment are poor choices to showcase. lodash is mostly moot with ES6 and beyond. date-fns is a cleaner version of moment.
3 comments

I disagree about lodash being "moot".

There are tons of things that lodash still provides that can't be easily replicated.

Just in the last month or so I've used throttle, debounce, uniqBy, memoize, get, and i'm sure more.

Sure, things like map, filter, find, reduce, etc... are made obsolete, and `Object.values` is a huge win to iterating objects or arrays with the same code, but lodash is far from useless.

And the way the library is structured, pulling in just one or 2 functions is fairly lightweight if you are using a bundler like webpack/rollup.

> Sure, things like map, filter, find, reduce, etc... are made obsolete

Not really. `_.map` for example works on Objects, and property access is very nice.

`_.map(object, 'myProperty.mySubProperty')`

vs

`Object.values(object).map(i => i.myProperty ? i.myProperty.mySubProperty : undefined)`

Yeah the iteratee shorthand stuff in lodash is massively convenient, however I'm starting to shy away from it in favor of more "obvious" approaches like your second example.

Although that being said, for deep property access like you have, i still reach for lodash's `get`, or more recently i'm beginning to use the new stage-1 proposal for "optional chaining" (also known as null-conditional operator in C#) [0].

That turns your second example into:

    Object.values(object).map(i => i.myProperty?.mySubProperty)
And honestly I might even reach for destructuring depending on the use case:

    Object.values(object).map(({myProperty}) => myProperty?.mySubProperty)
As nice as lodash's syntax is, it can be confusing for those who don't know it, and even I need to step back sometimes when I see it used heavily somewhere and almost replay what is happening in my head.

[0] https://github.com/tc39/proposal-optional-chaining

Object.values doesn't work in IE11, that can bite you really hard with “enterprise“ customers.
Luckily there are fairly simple polyfills that can be used for that (including using babel's preset-env system to configure it based on browser support!)
There are still heaps of tiny but useful utility functions in Lodash (`fromPairs`, `takeWhile`, ...). Sure those are trivial to code yourself, but with recent bundlers' tree-shaking abilities, it's just better practice to import a thoroughly tested version instead.
What about lodash's lazy evaluation/chaining/shortcut fusion? I don't think the ES6 standard library offers any alternative to it?