Why You Should Choose lodash-es Over lodash

As a software engineer, I’m always on the lookout for tools and libraries that can enhance my development workflow and optimize performance. One such tool is Lodash, a popular JavaScript utility library that makes working with arrays, objects, and other data structures a breeze. However, there’s an important decision to make when incorporating Lodash into a project: should you use the standard lodash package or opt for lodash-es? In this post, I’ll share my insights on why lodash-es might be the better choice for modern JavaScript development, especially when using TypeScript.

Thank me by sharing on Twitter 🙏

Introduction

Lodash has been a staple in the JavaScript community for years, offering a wide range of utility functions that simplify common programming tasks. However, as JavaScript has evolved, so too have the tools and practices we use. With the advent of ES modules (ESM) and advanced bundling techniques like tree shaking, the way we import and use libraries has changed significantly. This is where lodash-es comes into play.

Understanding the Differences

1. ES Module Support

  • lodash-es: This version of Lodash is designed to work with ES modules (ESM). It uses the native import and export syntax, making it a better fit for modern JavaScript environments that support ES modules.
  • lodash: The standard version of Lodash uses CommonJS module syntax, which is typical for Node.js environments but less optimal for modern front-end builds.

2. Tree Shaking

  • lodash-es: Supports tree shaking out of the box. Tree shaking is a technique used by modern JavaScript bundlers (like Webpack or Rollup) to eliminate unused code from the final bundle. By using lodash-es, you can ensure that only the parts of Lodash you actually use are included in your build, potentially reducing your bundle size.
  • lodash: While you can achieve some level of tree shaking with lodash by using tools like babel-plugin-lodash, it requires additional configuration and might not be as effective as using lodash-es.

3. Compatibility and Performance

  • lodash-es: Might be slightly better in performance for client-side applications due to the optimized module loading and tree shaking capabilities. It aligns better with modern JavaScript practices and tools.
  • lodash: Is more commonly used in server-side environments where the benefits of tree shaking and ES module syntax are less impactful.

4. Tooling and Ecosystem

  • lodash-es: Works seamlessly with modern build tools that support ES modules, like Rollup and newer versions of Webpack.
  • lodash: Has broader compatibility with older build systems and tools that expect CommonJS modules.

Practical Example: Migrating from lodash to lodash-es

Let’s walk through a practical example of migrating a project from lodash to lodash-es. Suppose we have a project that uses several Lodash functions.

Original Code with lodash

Here’s a sample code snippet using lodash:

JavaScript
const _ = require('lodash');

const array = [1, 2, 3, 4, 5];
const evens = _.filter(array, (num) => num % 2 === 0);
const doubled = _.map(evens, (num) => num * 2);

console.log(doubled); // Output: [4, 8]

Migrated Code with lodash-es

First install with Typescript types:

ShellScript
npm install lodash-es
npm install --save-dev @types/lodash-es

To migrate this code to use lodash-es, we’ll update the imports to use ES modules:

JavaScript
import { filter, map } from 'lodash-es';

const array = [1, 2, 3, 4, 5];
const evens = filter(array, (num) => num % 2 === 0);
const doubled = map(evens, (num) => num * 2);

console.log(doubled); // Output: [4, 8]

By making this change, we enable tree shaking, ensuring that only the filter and map functions are included in the final bundle. This reduces the bundle size and improves performance.

Conclusion

In modern JavaScript development, optimizing performance and efficiency is crucial. By choosing lodash-es over lodash, we can leverage ES modules and tree shaking to create leaner, faster applications. The seamless integration with modern build tools and alignment with current best practices make lodash-es the superior choice for many projects.

Switching to lodash-es is a straightforward process that can yield significant benefits in terms of bundle size and performance. As JavaScript continues to evolve, staying up-to-date with these optimizations helps ensure that our code remains efficient and maintainable.

Share this:

Leave a Reply