Subscribe to my email list now at http://jauyeung.net/subscribe/
Follow me on Twitter at https://twitter.com/AuMayeung
Many more articles at http...
For further actions, you may consider blocking this person and/or reporting abuse
To do the same with ES methods, just the first two examples:
_.times
_.debounce
These two example illustrate well that some things are easier to achieve without lodash than others. For me, the rule of thumb is: if I use a lodash method, I use it separately (never import all of it, and if possible, use the es version that can be tree-shaked!) and make sure I reuse it throughout the code so I actually get something in return.
In a surprisingly lot of cases I have seen, the use of utilities like lodash means outsourcing the deeper understanding of a problem to a library. While that can help speed up development in the short term, it might become a convenient habit, so much that one is tempted to overcomplicate the issue at hand in order to use these utilities when it could have been far simpler solved with native methods.
That does not mean you should not use lodash, but you should be conscientious about the implications.
This.
It makes me unsure if I should use
lodash.clonedeep
orlodash.merge
... If I write it myself, I can be sure of the implications.Also, lodash is too magical and too widely accepting in some methods, such as
lodash.filter
. (I saw a lot in lowdb.)A very nice example is
_.isEmpty()
: in most cases, you are testing if an object has no enumerable keys. You'll rarely need to check at the same time if the length of an array or a string is zero, but lodash will (needlessly in the stated case) check the input for all available types.This is one of the ones that we can implement ourselves without much effort.
I think the array's built-in
filter
method is pretty good so I never want to use the Lodash version.The problem with all the examples is
That'll pull in the entire lib. At this point, if you're looking for a utility, it should be very selective, much like your article suggests. That means your
npm install
should only pull in that utility, not the whole library.You're right. Just import the methods you need to use. That's more efficient.
Someone in another component has already shown how to implement
times
anddebounce
.Get can be achived with optional chaining, so heres how to dokeyBy
:...and with types:
(Have't tested this since I'm on mobile rigut now, but you yet the idea)
Conclusion
Since its doable in 1 line your statemet kinda false
To avoid
Object.fromEntries
, there is.reduce
.Actually, with map, filter, reduce -- you can do a lot.
It is impossible to say that you cannot do something in one-liner JavaScript, because minified JS is also a one liner.
I agree. I think there're things like
differenceWith
in Lodash that are harder to implement by ourselves and not available in the standard library.Can’t you now using optional chaining instead of
_get()
I'm not sure if it's finalized yet, but hopefully, it will be soon so that we don't need Lodash for it.
Thanks for the post, John! I'll admit, Lodash always felt "dirty" to me, especially since so many of its methods are now part of the ECMA standard or can be implemented in a few lines of native JavaScript. But the ones you highlighted are still very useful.
It's useful if you don't want to create your own function to do the things it does.
Very helpful! Thanks for sharing this worthy information.
Thanks for reading