Skip to content
IT glossary
FrontendUpdated December 3, 2026

Tree shaking

A build-time optimization that leaves unused JavaScript out of the files delivered to users, as long as removing it preserves the application's behavior.

What tree shaking removes

Suppose a dashboard imports one formatting helper from a library that exports dozens. The browser doesn't necessarily need to download all those helpers. Tree shaking lets the build tools leave unused code out of the bundle without editing the library's source. This can reduce both the download size and the amount of JavaScript the browser has to parse.

How the build knows what to keep

A bundler starts at the application's entry points and follows imports to build a dependency graph. It tracks which exports are referenced and what else must stay for the program to behave the same way. ES modules (ESM) make this easier because import and export describe connections the bundler can inspect without running the code. CommonJS can be harder to analyze: a module can assign to module.exports conditionally or compute which file to load with require().

An unused export doesn't mean the entire file is safe to drop. A module might register a custom element or install a polyfill as soon as it loads. An import such as import './register-widget.js' deliberately runs a module without importing any names. Webpack can use the sideEffects field in package.json to decide which modules it may skip when none of their exports are used. Setting "sideEffects": false for a package that relies on load-time effects can break a production build, for example by dropping stylesheet imports.

Example: an export the app never calls

// labels.js
export const upperLabel = text => text.toUpperCase();
export const lowerLabel = text => text.toLowerCase();

// main.js
import { upperLabel } from './labels.js';
console.log(upperLabel('ready'));

An optimized build can omit lowerLabel: nothing refers to it, and creating the function has no observable side effect. The named import makes the dependency clear, but it isn't a guarantee on its own. For instance, if a transpiler converts ESM to CommonJS before bundling, the bundler may no longer have the same information about which exports are used.

What to look for in an interview

A typical question is: “I switched to a named import. Why is the bundle still the same size?” Check which version of the dependency the bundler resolved, whether the module does work when it loads, and whether build optimizations are active. Inspect the production bundle or its build report to see what actually shipped; a development build may not run the same optimizations.

Keep the goals separate: tree shaking leaves out code the application doesn't need, code splitting puts code into separate chunks so some of it can load later, and minification makes the emitted code more compact. A tool can do more than one of these jobs. For example, a minifier may delete declarations after the bundler has determined that their exports are unused.