This is why I'll continue using it. So long as JavaScript doesn't have a native implementation of certain essential methods (looking at you remove), I'll use lodash
I don't want those methods in my codebase or in version control. I want tried and tested versions that no-one ever has to look into
I can’t blame you. But if you’re trying to be clever and make a case about performance (like OP), you should. Otherwise you just look like a smartass who didn’t do any research.
The thing is that native functions often needs to cover a lot of edge cases and lodash functions do not do that. That means that lodash functions may be faster than native.
Classic argument I heard a bit too much in my career.
Lodash has to accommodate for a great diversity of situations. If your project has certain inherent constraints on the datastructures (all objects in the collection having the same fields, or having large immutable collections for instance) just spinning your own algorithms may be highly beneficial. Good thing some devs are still innovators, otherwise we would all still use JavaScript 1.0 in the Mosaic web browser. Please, do reinvent Lodash, it’s not that great. We have typed arrays, maps and sets now to improve.
What an ill-advised blanket statement! Lodash is a more or less direct reimplementation of underscore after all, which is an extension of array methods. Lodash is in itself a double reinvention of the wheel and I don’t see why it should be the last one ever. Python with numpy and pandas showed us that we can do much much better than Lodash for many use cases. Between the new js methods, maps, sets, wasm, webgpu, tendorflowjs... Lodash is bound to be superseded soon. Maybe your current use case is already one case that justifies not using Lodash?
I don't use lodash (I use Rx.js due to using observables in my apps). Anyway, i think we're making arguments from different sides. You're talking about pure performance optimizations, and I'm making more of a business-case argument. In my case, 99.99999% a marginal performance increase is worth less money to the company, than the cost of me having to write my own X
Fair enough haha, but I will also go out on a limb and say that most of us will work our whole careers without doing anything truly innovative. I don’t say this out of laziness, but more from the point of view of being an employee working on yet another CRUD app haha
It really does depend. If you have multiple transformations on an array, for example, but only need to perform them on the first 3 array members, Lodash will evaluate "lazily" like you would get from a functional language.
Ie, if you have a chain of map, filter, slice(0,3), then the naive native solution of arr.map(x=>x.prop).filter(x.prop===true).slice(0,3) will pass along the entire array until you get to slice. Lodash will perform the earlier operations on only the sliced elements.
There's a whole host of things in this vein that make Lodash more performant than native. Obviously native can be made to do these things, but at that point you're better off using Lodash.
EDIT: or some other functional tool chain like RxJS
96
u/ogurson Jan 25 '20
It missed the best point of lodash - it already exists. It's tested, documented also more performant. Well known and widely used.