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

lodash Dependency #1085

Closed
mxro opened this issue Sep 22, 2017 · 4 comments
Closed

lodash Dependency #1085

mxro opened this issue Sep 22, 2017 · 4 comments

Comments

@mxro
Copy link

mxro commented Sep 22, 2017

I am interested in making cheerio as small as possible in terms of the footprint of its download size. There seems to be a pull request which specifies individual lodash module dependencies rather than all of lodash.

Using the lodash dependency declarations from this package, I was able to reduce the download size of cheerio significantly (by a few mbs, see the test project here).

 "lodash.bind": "^4.1.4",
 "lodash.defaults": "^4.0.1",
 "lodash.filter": "^4.4.0",
 "lodash.flatten": "^4.2.0",
 "lodash.foreach": "^4.3.0",
 "lodash.map": "^4.4.0",
 "lodash.merge": "^4.4.0",
 "lodash.pick": "^4.2.1",
 "lodash.reduce": "^4.4.0",
 "lodash.reject": "^4.4.0",
 "lodash.some": "^4.6.0",
 "lodash.assignin" : "^4.2.0"

Looking at the current version of package.json on the master branch, the lodash dependency declarations are reset to the complete lodash rather than the individual modules. Is there a reason why the individual module dependencies are not used?

(Please also see this related Stackoverflow question)

@billyjanitsch
Copy link
Contributor

billyjanitsch commented Sep 27, 2017

See the comments on #864, and #913.

tl;dr this would make cheerio smaller on its own, but would prevent deduping in the common case of having other dependencies which depend on Lodash. Also, it would load far more code at runtime. Also, the upcoming version of Lodash will do away with the zero-dependency modules entirely.

@mxro
Copy link
Author

mxro commented Sep 27, 2017

Thanks for your response. That's good to know!

One thing I didn't fully understand was this "it would load far more code at runtime". Could you explain this a little further?

@billyjanitsch
Copy link
Contributor

Sure. Many Lodash functions depend on each other. The zero-dependency modules (one per function) are compiled to inline their dependencies into a single file. Therefore, modules for functions A and B which both depend on C each contain an identical copy of C. If you depend on lodash.A and lodash.B, you load C twice. If you depend on Lodash and load lodash/A and lodash/B, you only load C once.

In practice, the dependency chains are very long. For instance, the source for lodash.map is over 2000 lines long, as is the source for lodash.filter, with most code being duplicated between them. Depending on 12 of them would mean loading something like ~10x as much Lodash code at runtime.

@fb55
Copy link
Member

fb55 commented Dec 25, 2017

Awesome response @billyjanitsch

@fb55 fb55 closed this as completed Dec 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants