Tree shaking is dead-code elimination during JavaScript bundling. A bundler reads your imports and exports, works out which code is never used, and leaves that code out of the production output. MDN’s glossary defines it as “the removal of dead code”, with bundlers using module imports and exports to decide what is used. It happens at build time, not in the browser, and it only works well when your code and your dependencies cooperate.
How tree shaking works
A bundler starts at your application’s entry points and follows the dependency graph. When modules use ES2015 import and export syntax, the bundler can often tell which exported bindings are actually used. It marks the unused ones, and the production minimizer then removes the statements it can prove are safe to drop. webpack’s guide demonstrates this with an unused exported function that disappears from the minified bundle. The example output is only “a few bytes smaller”, so treat it as an illustration, not a typical saving.
Tree shaking is not a browser feature. Tools such as webpack and Rollup do the work while generating output, so it does not clean up an arbitrary script at runtime.
Why ES module syntax matters
Static import/export statements let the bundler analyze the code without running it. If a compiler transforms those statements into CommonJS before the bundler sees them, the bundler has less static information and may keep more code. According to webpack’s guide, you should preserve ES module syntax through your toolchain up to the bundling step.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errors#1 Best Overall
Unused exports versus side effects
webpack treats two things separately, as its tree shaking guide explains:
- Used-export analysis (
usedExports): marks exports nobody uses. The code is actually removed only if the minimizer can prove the statements are safe to delete. - The
sideEffectsflag: declares whether importing a file does anything beyond providing exports. If a file is correctly marked side-effect-free and nothing from it is used, webpack can skip the whole module and its dependency subtree.
What counts as a side effect
Some modules matter even if the app uses none of their exports:
Rank #2
- CSS imports
- Polyfills
- Global registrations and event listeners
- Changes to prototypes
Setting "sideEffects": false in package.json is a correctness claim that importing any file in the package has no meaningful behavior apart from its exports. If the claim is too broad, the bundler may drop styles or setup code, which is the usual cause of “my CSS disappeared after tree shaking”. Instead, list the files that do have effects, including CSS, using patterns. webpack’s guide describes this approach.
Rollup’s equivalent controls
Rollup uses treeshake.moduleSideEffects. According to the Rollup configuration documentation, the default is true. Setting it to false makes Rollup assume that modules from which nothing is imported have no other effects. Rollup warns that this can remove setup modules, polyfills or styles. Rollup core does not read a package’s sideEffects field itself. The node-resolve plugin can read it and set per-module behavior. Check your Rollup version and plugin setup before relying on these details.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →A practical workflow
- Measure first. MDN recommends using the browser’s network and performance tools to find real bottlenecks before optimizing.
- Keep ES module syntax intact until the bundler runs.
- Build in production mode so the minimizer runs.
- Declare side effects honestly: list CSS, polyfills and initialization files rather than flagging the whole package.
- Test a minimal production build that imports one component. Inspect the generated bundle, then confirm that styles and behavior still work.
Tree shaking versus related techniques
| Technique | What it targets | When it happens |
|---|---|---|
| Tree shaking / dead-code elimination | Unused exports, modules or statements that can be proven unnecessary | Build |
| Minification | The characters in the code that remains | Build |
| Compression (gzip, Brotli) | Bytes sent over the network | Transfer |
| Code splitting / deferred loading | When separate pieces of JavaScript load; nothing is deleted | Runtime loading |
These are distinct and are normally combined, as MDN’s JavaScript performance guide explains.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.What to expect from the results
Removing unused JavaScript can reduce the code transferred, parsed and executed. None of the documentation reviewed gives a general percentage or guaranteed speedup, so the benefit depends on how much of your dependencies you actually use. Verify it by comparing bundle size and load behavior before and after.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




