tree shaking
{{Short description|Dead code elimination technique}}
In computing, tree shaking is a dead code elimination technique that is applied when optimizing code.{{cite web | url=https://developers.google.com/web/fundamentals/performance/optimizing-javascript/tree-shaking#what_is_tree_shakingv| title=Reduce JavaScript Payloads with Tree Shaking }} Often contrasted with traditional single-library dead code elimination techniques common to minifiers, tree shaking eliminates unused functions from across the bundle by starting at the entry point and only including functions that may be executed.{{cite web |last1=Harris |first1=Rich |title=Tree-shaking versus dead code elimination |date=15 January 2019 |url=https://medium.com/@Rich_Harris/tree-shaking-versus-dead-code-elimination-d3765df85c80 |accessdate=16 September 2020}}{{cite web |last1=Ladd |first1=Seth |url=http://blog.sethladd.com/2013/01/minification-is-not-enough-you-need.html |title=Minification is not enough, you need tree shaking |website=Seth Ladd's Blog|date=29 January 2013 }} It is succinctly described as "live code inclusion".
History
Dead code elimination in dynamic languages is a much harder problem than in static languages. The idea of a "treeshaker" originated in LISP[https://groups.google.com/forum/#!topic/comp.lang.lisp/pspFr1XByZk comp.lang.lisp What's a treeshaker?] in the 1990s. The idea is that all possible execution flows of a program can be represented as a tree of function calls, so that functions that are never called can be eliminated.
The algorithm was applied to JavaScript in Google Closure Tools and then to Dart in the dart2js compiler also written by Google, presented by Bob Nystrom in 2012[http://www.cio.com/article/2382855/developer/can-google-dart-solve-javascript-s-speed-and-scale-problems-.html Can Google Dart Solve JavaScript's Speed and Scale Problems?] and described by the book Dart in Action by author Chris Buckett in 2013:
{{Quote|text=When code is converted from Dart to JavaScript the compiler does 'tree shaking'. In JavaScript you have to add an entire library even if you only need it for one function, but thanks to tree shaking the Dart-derived JavaScript only includes the individual functions that you need from a library|author=Chris Buckett}}
The next wave of popularity of the term is attributed to Rich Harris's Rollup project[https://web.archive.org/web/20190731133056/https://www.engineyard.com/blog/tree-shaking How To Clean Up Your JavaScript Build With Tree Shaking] developed in 2015.
Relation to ECMAScript 6 modules
The popularity of tree shaking in JavaScript is based on the fact that in contrast to CommonJS modules, ECMAScript 6 module loading is static and thus the whole dependency tree can be deduced by statically parsing the syntax tree. Thus tree shaking becomes an easy problem. However, tree shaking does not only apply at the import/export level: it can also work at the statement level, depending on the implementation.{{citation needed|date=December 2017}}
References
{{reflist}}