Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the size impact of lodash by picking the required modules only #41

Merged
merged 1 commit into from Dec 14, 2022

Conversation

kiskoza
Copy link
Contributor

@kiskoza kiskoza commented Dec 14, 2022

PR Checklist

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller
    PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into
    two PRs otherwise).
  • This PR's title starts is concise and descriptive.

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or
    fixes.
  • I've updated any docs, .md files, etc… affected by this change.

What

Change import { module } form "lodash" lines to import module from "lodash/module" to reduce the size of the bundle.

Why

Currently the code bings the whole lodash module to the final browser bundle which ends up being bigger than it should be. If you import only the required packages, you can reduce the size of the final bundle.

Steps to reproduce:

yarn build:import
yarn build:browser
ls -lah dist/browser

On the main branch it generates a 105K file for index.js, with this fix the file is only 67K

Known limitations

N/A

@kiskoza kiskoza requested a review from fnando as a code owner December 14, 2022 15:27
Copy link
Owner

@fnando fnando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thanks!

@trajano
Copy link

trajano commented Dec 23, 2022

Interesting because with tree shaking at least on React-native projects there's no change because of tree shaking.

Assuming lodash 5.0 ever comes out they already marked the use as deprecated https://lodash.com/per-method-packages

But since the OP has proof otherwise I guess it's fine.

trajano added a commit to trajano/react-hooks that referenced this pull request Dec 23, 2022
Per https://lodash.com/per-method-packages

 if you import or require methods directly, e.g. const throttle = require('lodash/throttle'), only the subset of lodash code your package uses will be bundled in projects that use your package.

fnando/i18n#41 also has done it and had a
measurable decrease
@fnando
Copy link
Owner

fnando commented Dec 24, 2022

@trajano I think we're good, as we're doing exactly what they're suggested on that link you sent: importing individual functions, but out of the lodash package (rather than individual packages).

The size reduction was measured against the browser bundle, so I can't tell you it'd also reduce react-native's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants