Open In App

What is Tree Shaking in JavaScript ?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Tree shaking is a JavaScript optimization technique aimed at eliminating dead or unused code from a project, specifically in the context of bundling with tools like Webpack or Rollup. The term “tree shaking” originates from the context of shaking a tree to make dead leaves fall.

In the context of JavaScript, tree shaking relies on the ES2015 module syntax, which allows for static analysis of the code dependencies. During the bundling process, tree shaking identifies and removes unused or dead code paths, reducing the size of the final bundle. This optimization is particularly beneficial for large-scale applications or libraries where only a portion of the codebase is needed for a specific functionality.

To enable effective tree shaking, code must be written in a modular and side-effect-free manner. Additionally, dependencies need to be imported using the ES2015 module syntax to allow static analysis. Tree shaking enhances the performance of web applications by delivering smaller bundles, resulting in faster load times and improved runtime efficiency.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads