
Tree Shaking vs Minification: What Each One Does
Minification shortens the JS you keep. Tree shaking drops unused modules. See the difference, Vite/Webpack setup, and when to use both.
Two techniques people mix up
Optimizing a modern frontend means hearing “minify your bundles” and “tree-shake your bundles” all the time. They are not synonyms. Minification shrinks code that is already in the final file (whitespace, shorter identifiers). Tree shaking removes modules that are never used from the dependency graph. Both cut the bytes transferred, at different stages of the pipeline. For a single file without a bundler, an online JavaScript minifier is often enough. A full application usually combines static import analysis, a production bundler and a minifier (Terser, esbuild, SWC or Oxc). This guide gives the definitions, a comparison table, measured sizes and a Vite and Webpack setup. For deeper tuning, see our JavaScript minification guide.
Minification and tree shaking do different jobs
Minification works on a file whose contents are already decided. It removes whatever the JavaScript engine does not need (whitespace, comments and, depending on the tool, local variable names). It cannot tell whether an exported function will matter later if it is already in the bundle. For the fundamentals and a comparison of tools, see our complete JavaScript minification guide. When you need readable code again, keep an online JavaScript unminifier or the Beautify mode at hand.
Before
After
Tree shaking relies on ES modules (`import` / `export`) and static analysis. Anything unreachable from an entry point can be dropped before minification runs. Libraries must ship compatible builds (`sideEffects` in `package.json`, no hidden global side effects). The most telling fix is to replace a barrel or default import with a precise import.
Before
After
CSS has no ES-module tree shaking like JavaScript. PostCSS, Tailwind and PurgeCSS do the same job instead: they remove unused rules before you run an online CSS minifier or cssnano on the final output.
Measure what actually moves in your bundle
The two techniques show up in different parts of a bundle report. Tree shaking cuts the number and size of modules. Minification shrinks the bytes of the modules that remain. In a test with Vite 8.3.4 and only the debounce function of lodash-es, the bundle weighed 85.0 KB minified with tree shaking turned off. It weighed 13.9 KB shaken but not minified, and 2.9 KB with both. Use a treemap to catch regressions after dependency updates.

Build in production mode: `vite build` and Webpack's `mode: 'production'` set `NODE_ENV` for you. With other tools, set `NODE_ENV=production` yourself. Without it, some bundlers skip optimizations and libraries such as React keep their development code.
The two techniques side by side
Use this when you size an optimization ticket. First ask whether the code should be in the graph at all. Then ask how to shrink its text.
Minification
Tree shaking
These checks keep an optimization from staying on paper.
Minify a standalone file
When Webpack or Vite is not set up yet, or when you ship a single script, paste your JavaScript into our client-side tool. It returns compact code and a Beautify mode to read it again.
Paste or load your file
Use the online JavaScript minifier. Your code stays in your browser.
Switch to Beautify
Switch to Beautify for a readable layout. This is handy before you copy the code back into your toolchain.
Do not confuse it with shaking
The tool minifies the files you give it and does not analyze which modules are used. Dropping unused modules takes a production bundler working on the module graph.
As your codebase grows, follow the advice in our online minifiers vs build tools guide. The online tool stays useful for exceptions and quick checks.
Combine tree shaking and minification in builds
`vite build` applies tree shaking and minifies with Oxc by default (Vite 8, built on Rolldown). Vite 7 and earlier used Rollup and esbuild. Keep your imports ES-module friendly.
Configuration
Usage
`mode: 'production'` turns on `usedExports`, `sideEffects` and minification with Terser. Check that the libraries you use declare `sideEffects` accurately.
Configuration
Usage
Chain the Tailwind or PurgeCSS purge, then minification, as in our CSS minification guide. The purge does the shaking, and minification then shrinks the bytes of what is left.
Basic example
File minification
Conclusion
Tree shaking and minification complement each other. One trims the dependency graph, the other writes what remains in fewer bytes. Without tree shaking, you ship dead modules. Without minification, you ship verbose output for an already slim graph. An online minifier covers snippets outside your repository. Automate your bundler for everything else. Keep FastMinify at hand for edge cases.
Minify your JavaScript in your browser
Related Articles

Paste a token and a JWKS to check an RS256, ES256 or HS256 signature in your browser. How kid picks the key, measured errors and the claims to check.

HTTP/2 and HTTP/3 change how files travel, not how big they are. Measured sizes, compile times and bundling tests show what minification still saves.

Convert a Unix epoch to a readable date and back, tell seconds from milliseconds, and avoid time zone, JWT and clock change traps, all in your browser.