DEV Community

Cover image for 14 functions I made to dump lodash and reduce my bundle size...

14 functions I made to dump lodash and reduce my bundle size...

Mike Talbot ⭐ on August 16, 2021

Lodash and underscore changed the way I write Javascript forever, but today there might be better options for the most common functions. I recentl...
Collapse
 
artydev profile image
artydev

Great as always :-)

Collapse
 
hasnaindev profile image
Muhammad Hasnain

I have personally never used lodash because there has never been a need for me to do so. I just want to point out that lodash is tree shakable and will not contribute to bundle size a lot expect for the functions you've imported of course.

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

See my point about the decisions it makes around polyfilling. For these 14 functions with lodash-es - which is better designed for tree shaking - I still saved a significant amount of bundle size.

Collapse
 
living_syn profile image
Jeremy Mill

your implementation of merge is vulnerable to prototype pollution. You should go and read lodash's implementation before re-implementing it.

portswigger.net/daily-swig/prototy...

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Thanks :) Wilco

Collapse
 
robertomaurizzi profile image
Roberto Maurizzi

I recently added lodash to a project I'm working on.Docs are inconclusive, some recommendations I found about importing lodash functions from submodules do not work (either they were using the now deprecated standalone submodules, or something changed somewhere).
In the end I added a babel module designed to strip away the unused pieces of lodash and that gave me a very decent subset of it in my bundle.
Again, my impression? The Javascript ecosystem is a royal mess that can continue only because it has no competition.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Here here to that... :)

Collapse
 
quozzo profile image
Quozzo

I'm not sure if this is a smaller implementation of your merge function, except mine returns a new value instead of mutating the first value passed in.

gist.github.com/Quozzo/3715ad741cf...

If you use Object.assign with an empty object/array as the first argument then it should be a non-issue either way.

Collapse
 
6temes profile image
Daniel

So... What is exactly the difference between using functions from Lodash and using your own function? Is your code lighter than Lodash just because you wrote it?

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Well see above, partly down to the fact that I wrote tighter implementations with only the functionality I needed but a lot down to the face I used the same bundler to add polyfills I already need for the ones there. The different was around 22kb reduction from the tree shaken version. TBH debounce is probably the killer in there. But there are lots of "BaseClone" etc. Code that would work anywhere but I certainly don't use.

As you can see, this isn't all of lodash-es... so tree shaking worked as far as possible:

Build Analysis

Collapse
 
6temes profile image
Daniel • Edited

Then, to be fair, you didn't write functions that would replace Lodash. You wrote functions that offer a subset of the functionality offered by Lodash, and that's why they are lighter.

But something that it's true is that it would be really nice if they released a version 5 of Lodash that would be compatible only with evergreen browsers. I am sure that they could get rid of a lot of code.

Thread Thread
 
miketalbot profile image
Mike Talbot ⭐

Yeah I did document above which functions didn't work the same. Debounce, get and set basically. I don't have the need for the advanced versions of those.

Collapse
 
abhishekraj272 profile image
Abhishek Raj

Better to tree shake it to decrease its size

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

Didn 't work see above. The libraries were still much bigger than my implementations.

Collapse
 
aminnairi profile image
Amin

Your get function won't return the value if it is 0 (or any other falsy value). You'll get the defaultValue every time (which may be something else like "none").

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Good point, fixed.

Collapse
 
jasoncubic profile image
JasonCubic
Collapse
 
miketalbot profile image
Mike Talbot ⭐

See the earlier article in the series for how we use currying rather than the lodash method.

Collapse
 
xpuu profile image
Martin Kase

Nice. I use these: github.com/angus-c/just