jest-dom icon indicating copy to clipboard operation
jest-dom copied to clipboard

chore: replace lodash with lodash.isequalwith

Open wojtekmaj opened this issue 1 year ago • 5 comments

What:

Continues effort from https://github.com/testing-library/jest-dom/pull/593; further improves on https://github.com/testing-library/jest-dom/issues/592

Why:

Since we're only using one method from the entire lodash package, we can dramatically reduce @testing-library/jest-dom's install footprint (by roughly 1.3 MB) by replacing lodash (https://packagephobia.com/result?p=lodash) with lodash.isequalwith (https://packagephobia.com/result?p=lodash.isequalwith).

How:

Checklist:

  • [ ] Documentation N/A
  • [ ] Tests N/A
  • [ ] Updated Type Definitions N/A
  • [x] Ready to be merged

wojtekmaj avatar Sep 13 '24 20:09 wojtekmaj

https://lodash.com/per-method-packages

These per method packages are deprecated. Most projects already have lodash installed via transient dependencies anyway.

chyzwar avatar Sep 26 '24 07:09 chyzwar

This is a vicious circle. lodash is being installed everywhere because it's already installed everywhere.

wojtekmaj avatar Sep 26 '24 09:09 wojtekmaj

Yes someone has to start this, we can't all go around pointing fingers at the other packages saying "we won't optimize because the other's aren't".

The per-function modules are indeed deprecated, but I think it's the best we can do right now with lodash.

JReinhold avatar Oct 02 '24 07:10 JReinhold

The problem with per function modules is that they bloat the runtime of the program. For example, this is source code for module in question https://www.npmjs.com/package/lodash.isequalwith?activeTab=code and this is when imported from lodash via modular import https://github.com/lodash/lodash/blob/main/src/isEqualWith.ts

If we look how lodash is used across this organization: https://github.com/search?q=org%3Atesting-library%20lodash&type=code

There are multiple lodash packages installed, both full version of lodash and per module. Some of per module packages were not updated in 8 years and might be outdated. https://www.npmjs.com/package/lodash.groupby?activeTab=readme

I would prefer that my dependencies of my projects have the small number of well vetted transient dependencies. Install size is secondary, because savings of 80kB is nothing compared to esbuild/swc/bione/prisma 50MB+ packages sizes. Solution for large node_module is caching, not million of smaller packages.

If we want to optimize, we should use lodash/<functionName> everywhere. This would make runtime cost small and be compatible with future lodash v5.

Saying all that, runtime cost would be both influenced by how often module is required (lodash/isEqualWith have higher require cost). If lodash function is loaded once during test run vs loaded on every test file.

chyzwar avatar Oct 02 '24 11:10 chyzwar

Then maybe we should take a different turn and migrate to es-toolkit across all the repos?

wojtekmaj avatar Oct 02 '24 12:10 wojtekmaj

yes you can see es-toolkit migration at this PR https://github.com/testing-library/jest-dom/pull/671

okinawaa avatar Jul 16 '25 03:07 okinawaa

Superseded by #676

MichaelDeBoey avatar Aug 05 '25 10:08 MichaelDeBoey