Member-only story
Stop Importing Whole JavaScript Packages
Reduce bundle size by importing only the parts of a package you need
How many times have you imported a complete package but only need very few features of the features from that package? Probably, a lot.
The most common example of this scenario Lodash. For those of you, who haven’t heard of it, you should check out the documentation.
“Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.” — Lodash.com
It has more than 200 functions, which means it really takes out all of the hassle. However, you might end up using only 4-5 in your project, which makes me wonder should I really import the whole package when I’ll only be using 2-3% of the package?
One of my absolute favourite functions is get
, which lets you safely access nested objects and also provides default values.
This keeps your code much cleaner and avoids bugs because when you’re expecting an empty array, it won’t return null…