Homeglossary

Tree Shaking

Tree shaking is a build-time optimization that removes unused code from a software application's final bundle. It helps web and mobile apps send less JavaScript to users by retaining only code the application can be shown to use. It works best with statically analyzable ES modules and code that avoids hidden side effects.

Build leaner production apps with Emergent.

What Is Tree Shaking in JavaScript?

Tree shaking is a build-time optimization that removes unused code from an application's final JavaScript bundle. Also called dead-code elimination, it keeps exports the build tool can prove the app uses and discards the rest. MDN's tree shaking reference explains the process for modern JavaScript modules.

How Tree Shaking Works

Tree shaking runs during a production build. Bundlers such as webpack, Rollup, Vite, and esbuild can perform it.

  1. The bundler starts from application entry points.
  2. It analyzes static ES module imports and exports to map dependencies.
  3. It marks reachable functions, classes, and modules as needed.
  4. It preserves modules with important loading side effects.
  5. It removes unused code and often minifies the remaining bundle.

Tree Shaking vs. Minification vs. Code Splitting

These optimizations work together but solve different problems.

TechniquePurposeOutcome
Tree shakingRemove code that is not usedFewer modules or exports in the bundle
MinificationMake retained code more compactSmaller file size, with the same behavior
Code splittingLoad code only when a route or feature needs itMultiple files and less initial loading

Benefits of Tree Shaking

Its value depends on an application's dependencies and usage.

  • Smaller bundles can reduce JavaScript downloads.
  • Less code may reduce browser parsing and execution on first load.
  • Specific imports encourage a focused dependency footprint.
  • With code splitting, it can keep rarely used features out of the initial bundle.

Practical Limits of Tree Shaking

A bundler removes code only when it can safely do so.

  • CommonJS patterns and runtime-dependent property access are harder to analyze than ES modules.
  • Dynamic behavior can hide which exports an app will use.
  • Barrel files can load side-effectful modules or obscure intended imports.
  • Top-level work, such as registering a plugin or loading a stylesheet, may need to remain.

How to Make Code Tree-Shakable

Application and library authors can make safe removal easier.

  • Prefer ES modules and import specific exports.
  • Avoid unnecessary top-level work in reusable modules.
  • Declare package side effects accurately.
  • Offer separate entry points for large, independent features when useful.
  • Inspect production bundle reports after adding major dependencies.

Checking Whether Tree Shaking Worked

Import one date-formatting utility instead of an entire library, then create a production build and inspect it with a bundle analyzer. Confirm unused exports are absent and test the app carefully. A broad side-effect setting can remove required code. This review is useful throughout app design and development.

Tree Shaking in Apps Built With Emergent

Full-stack web and mobile apps benefit from production build optimization after features are generated. When using Emergent as an AI app builder, review client dependencies and production bundles so users receive only the code their experience needs. Build leaner production apps with Emergent.

Start Building
on Emergent today
Start Building