How to Import Lodash in your JavaScript Projects for Lowest Bundle Size

How to correctly include specific functions of the popular lodash library in your web JavaScript project without having to import the entire library.

Lodash is an extremely popular JavaScript library that provides a lot of useful functions for working with strings, arrays and objects in your web projects.

Some of the Lodash functions are now supported natively in modern JavaScript, but the library still adds value and saves you time.

For instance, if you want to generate a random number between 1 and 10, the _.random(1, 10) function is a great way to do it, similar to the RANDBETWEEN function of Google Sheets. The _.shuffle() function can help you quickly shuffle an array of values.

The Correct Way to Include Lodash

If your JavaScript project requires Lodash, you can include the library in your code in 4 different ways.

1. Import the entire lodash library

import _ from 'lodash';

const capitalizeFirstName = (name) => {
  const result = _.capitalize(name);
  console.log(response);
};

2. Import using named aliases

import { capitalize } from 'lodash';

const capitalizeFirstName = (name) => {
  const result = capitalize(name);
  console.log(response);
};

3. Import specific methods by path

import capitalize from 'lodash/capitalize';

const capitalizeFirstName = (name) => {
  const result = capitalize(name);
  console.log(response);
};

4. Use per-method lodash packages

import capitalize from 'lodash.capitalize';

const capitalizeFirstName = (name) => {
  const result = capitalize(name);
  console.log(response);
};

Which importing method would result in the lowest bundle size?

The option #1 will include the entire lodash library in your output bundle and is not recommended. The second option will also import the full library and should be avoided.

The #4 method of importing per-method lodash packages will result in the lowest bundle size, but it is not recommended since this approach will be deprecated in the future versions of lodash.

The approach #3 is recommended since it will only import the specific Lodash methods you need and also reduce the bundle size.

Bonus Tip: Memoization with Lodash

The Lodash library includes a memoization method called _.memoize() which is useful for caching expensive functions.

import memoize from 'lodoash/memoize';

const expensiveFunction = (input) => {
  return input * input;
};

const memoizedFunction = memoize(expensiveFunction);

console.log(memoizedFunction(5)); // Calculates the square of 5
console.log(memoizedFunction(5)); // Returns the cached value

There’s however a big limitation of memoization with Lodash - it will only use the first parameter of the function as the cache key and ignore the rest. Let me explain.

const add = (a, b) => {
  return a + b;
};

const memoizedAdd = _.memoize(add);
console.log(memoizedAdd(1, 2)); // Calculates the sum of 1 and 2 and caches the result
console.log(memoizedAdd(1, 3)); // Returns the cached value which is 3 (incorrect)

As you may have noticed, the second parameter of the function is ignored and thus the result is incorrect since it returned the cached value based on the first parameter itself.

Memoization with Multiple Parameters

To fix this problem, you can use an alternative memoization library like fast-memoize or you can add a resolver function to the memoization method as shown below.

const multiply = (a, b) => {
  return a * b;
};

const resolver = (...args) => {
  return JSON.stringify(args);
};

const memoizedMultiply = _.memoize(multiply, resolver);

console.log(memoizedMultiply(1, 2)); // Calculates the product of 1 and 2 and caches the result
console.log(memoizedMultiply(1, 3)); // Calculates the product of 1 and 3 and caches the result
console.log(memoizedMultiply(1, 2)); // Returns the cached value
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.