berry icon indicating copy to clipboard operation
berry copied to clipboard

chore: replace lodash with es-toolkit

Open wojtekmaj opened this issue 1 year ago • 7 comments

What's the problem this PR addresses?

es-toolkit is a modern lodash alternative that claims to be much faster and lighter, potentially improving Yarn overall performance: https://es-toolkit.slash.page/intro.html

How did you fix it?

The lazy way, frankly: using es-toolkit/compat to provide practically drop-in lodash replacement with minimal effort.

Checklist

  • [x] I have set the packages that need to be released for my changes to be effective.
  • [x] I will check that all automated PR checks pass before the PR gets reviewed.

wojtekmaj avatar Sep 13 '24 21:09 wojtekmaj

Some of our dependencies are using lodash so this would increase the install size and maybe the bundle size.

merceyz avatar Sep 13 '24 22:09 merceyz

Valid point, but it seems like not too many dependencies are in fact using lodash. It should be relatively easy to clean this whole tree up. In the meantime, this draft PR is in itself ready to go I believe. :)

wojtekmaj avatar Sep 14 '24 19:09 wojtekmaj

Well, this PR DOES make Yarn smaller by a bit:

Before: 2.63 MiB https://github.com/yarnpkg/berry/actions/runs/12011432883/job/33480410817#step:5:27

After: 2.61 MiB image

I believe that's because while other dependencies ~~like ink~~ (not anymore) are indeed using lodash, they, just like Yarn, are importing modules selectively from it, leading to better tree shaking than one could initially assume.

1% improvement is not much but it certainly adds up 🤷

I also see a potential of shaving off further 2 KB, but I'd rather do it in small stages.

wojtekmaj avatar Sep 14 '24 20:09 wojtekmaj

while other dependencies like ink are indeed using lodash, they, just like Yarn, are importing modules selectively from it

Because I patched it to do that, in the future we could change that patch to use es-toolkit instead. https://github.com/yarnpkg/berry/blob/fb6d2d696f36040bb082ddd20adffab67eaa59dc/.yarn/patches/ink-npm-3.0.8-3a8005f59f.patch

merceyz avatar Sep 14 '24 21:09 merceyz

I couldn't forbid @types/lodash because it appears to be deeply rooted inside pkg-tests-specs, but lodash alone was fine :)

wojtekmaj avatar Sep 14 '24 22:09 wojtekmaj

Update: the latest version of ink, our dependency, also switched from lodash to es-toolkit, making this PR more welcome than before, IMO. I'll work in getting my work rebased.

wojtekmaj avatar Dec 02 '24 11:12 wojtekmaj

I think @wojtekmaj had addressed all the review comments—any reason the review isn’t proceeding?

Also, since some time has passed, I think it would be good to update the es-toolkit version.

dayongkr avatar May 10 '25 06:05 dayongkr

I can re-review if you can fix the conflicts first!

RDIL avatar Jul 14 '25 18:07 RDIL

@RDIL Done! :)

BTW, this PR still does its job, still decreases binary size.

Before: 2.86 MiB https://github.com/yarnpkg/berry/actions/runs/15427018142/job/43416399142#step:5:27

After: 2.84 MiB

➤ YN0000: ✓ Done building the CLI! ➤ YN0000: ? Bundle path: /workspaces/berry/packages/yarnpkg-cli/bundles/yarn.js ➤ YN0000: ? Bundle size: 2.84 MiB ➤ YN0000: ? Bundle version: 4.9.2-git.20241202.hash-693272998

wojtekmaj avatar Jul 15 '25 07:07 wojtekmaj

@RDIL thank you!! Are we waiting on one more review to merge this? Not sure if it's a stale request or not ❤️

spanishpear avatar Jul 26 '25 03:07 spanishpear