Skip to content
BENIBIX Learn

Tree Shaking

Development🌐 Web DevelopmentBuild Tools

Also known as Dead Code Elimination, Tree-Shaking

A build step that drops code you imported but never used.

Think of it like

Think of it like shaking an apple tree so the dead leaves fall off. Only the living parts stay.

Example

Emma imports one date function from a large library. Tree shaking keeps that one function and drops the other 200.

Why it matters

It keeps bundles small even when you use big libraries. Smaller bundles mean faster pages.

Definition

Tree shaking is a build step that removes code you import but never use. A bundler follows which functions each file really calls. Anything that is never reached is left out of the final bundle. This matters because big libraries may hold hundreds of functions when you use three. It works best with modern import and export syntax.

Common mistake

Importing a whole library with one star import. Some tools then cannot tell what you use, and keep it all.

Key takeaways

  1. 1
    Unused code goes. Only what is called stays.
  2. 2
    Done by the bundler. At build time.
  3. 3
    Needs clean imports. Import only what you use.

Compare