05 May 2020

Importing an individual lodash function

Let's say you only need to import a single function of Lodash instead of a whole package. And really, what's the reason of importing so many functions if you only need Debounce for example?

You may have tried...

import { debounce } from "lodash";

... and that's right, it will import debounce but It will import every single other function too, you could use _.throttle for example.

That's not what you want.

To import a single Lodash function you have to do it like this...

let debounce = require("lodash/debounce");

This will only load debounce and you could use it like debounce(f, x) and cut a few kB from your bundle in the process.