TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Tree Shaking

36 pointsby hidden-spyderalmost 4 years ago

4 comments

jamesgreenleafalmost 4 years ago
1. In real life, you shake real trees, to get fresh fruit.<p>2. In JS, you shake fake trees, to shed bad fruit.<p>3. Common wisdom says: &quot;Ye shall know them by their fruits.&quot;<p>Therefore, all JS dependency trees are rotten. &#x2F;s
评论 #28026715 未加载
评论 #28027008 未加载
评论 #28027429 未加载
akerstenalmost 4 years ago
Tree shaking is such a well-named concept that I could tell you exactly what it was the first time I heard someone say it, even though they hadn&#x27;t defined it for me yet. It&#x27;s such a joy when we can map abstract mechanisms to plain language so easily.
xg15almost 4 years ago
One issue that I haven&#x27;t seen discussed often yet are &quot;dead dependencies&quot; - something where I believe tree shaking is essential.<p>Image the following scenario: You have an app A that pulls in library B as a direct dependency. B pulls in another library C as a transitive dependency. C may have further transitive dependencies of its own (D, E, F...)<p>As it happens, B is a &quot;toolbox&#x2F;commons&#x2F;utils&quot; library, which bundles several small, unrelated bits of functionality into a single asset for convenience. A only uses a fraction of B&#x27;s functionality and it does <i>not</i> use the parts of B that require C.<p>So not only does B suddenly contains lots of dead code, the <i>entire library C and all of transitive dependencies</i> are dead code. None of those libraries are actually used by anything and A&#x27;s developers will likely have no idea what they do at all - yet the build system considers them essential and will bundle them with the application.<p>There is one escalation of this, which I guess you could call &quot;undead code&quot;: Some frameworks (e.g. Spring for Java) use classpath scanning or similar mechanisms to automatically detect <i>and execute</i> modules across your codebase. This scan includes modules pulled in through transitive dependencies!<p>So in the worst case, a library that you never consciously added to your project and that has no actual reason to be there <i>will execute code</i> when your app is running.
评论 #28027885 未加载
评论 #28027216 未加载
评论 #28031113 未加载
thosakwealmost 4 years ago
I wonder why tree-shaking wasn&#x27;t always the default for, say, JS bundlers. If a compiler&#x2F;analyzer knows what the entry point of a program is, as well as any symbols it exports to the outside world, isn&#x27;t it relatively simple to figure out what&#x27;s not being used?<p>I could be misunderstanding something.
评论 #28026243 未加载
评论 #28026281 未加载
评论 #28026777 未加载