Why I Hate Lodash
·
2 min read
During project development and code reviews, I consistently recommend that teams reduce their dependence on Lodash and use native JavaScript instead. Whenever I see someone using it, I pay attention and repeatedly ask why they need Lodash. Here I’ll explain why I dislike it so much.
- Lodash provides many utility functions, both those you know and those you don’t. Overuse leads to over-dependence, and you may end up
knowing Lodash but not native JS, preventing proper learning of native JavaScript due to heavy Lodash usage
- Depending on Lodash realistically means
increased bundle size
, though with today’s high bandwidth and fast internet speeds, this size increase might not be a major concern - When both native methods and Lodash can accomplish the same functionality, using Lodash may mean
worse performance
. - Lodash methods consider many edge cases comprehensively, so they have
stronger error tolerance
, but you should know that not throwing errors is sometimes more dangerous than throwing them. For example, with find and get methods - no error doesn’t necessarily mean correctness. - Some Lodash methods like the get method actually cause
type information loss
, and path access parameters lose static analysis capabilities, making things like refactoring harder to detect. - Some Lodash methods can cause errors if you’re not familiar with them. For example, with isEmpty, you should know that
_.isEmpty(true)
returns true
Attitude Towards Using Lodash
- For specific functionality, prioritize native JavaScript approaches, considering target browser compatibility
- When importing Lodash, use ES module tree-shaking imports to reduce bundle size to some extent
- I recommend this repository - You-Dont-Need-Lodash-Underscore. Through this repository, you can understand how experts view Lodash and learn how to implement equivalent functionality natively
- I’m not saying don’t use it, but I resent and reject mindlessly using Lodash to solve problems without thinking, because I believe these people don’t consider whether native solutions exist or think about performance - to put it emotionally, they have no sense of guilt.
- For those blind Lodash fans, what will you do someday when there’s no Lodash dependency in your development environment?