
Tree shaking vs minification: optimize your bundle
Understand how tree shaking and JavaScript minification differ — two complementary ways to shrink bundles and speed up your site.
Two techniques people mix up
When optimizing a modern frontend you constantly hear “minify” and “tree-shake” your bundles. They are not synonyms: minification squeezes code that is already in the final file (whitespace, shorter identifiers), while tree shaking removes modules that are never used from the dependency graph. Both cut transferred bytes, but at different pipeline stages. For a quick pass on an isolated file (no bundler), an online JavaScript minifier is often enough; for a full application you typically combine static import analysis, a production bundler, and a minifier (Terser, esbuild, SWC…). This guide clarifies definitions, offers a comparison table, and shows realistic Vite/Webpack-style setup — pointing to our JavaScript minification guide for deeper tuning.
Minification and tree shaking do different jobs
Minification works on a file whose contents are already decided: it strips anything redundant for the JS engine (whitespace, comments, aggressive local renaming depending on the tool). It cannot infer whether an exported function will “eventually” matter if it is already bundled in. For fundamentals and tooling comparisons see our complete JavaScript minification guide. When you need readability again, keep an online JavaScript unminifier or Beautify handy.
Before
After
Tree shaking relies on ES modules (`import` / `export`) and static analysis: anything unreachable from an entry can be dropped before minification runs. Libraries must ship compatible builds (`sideEffects` in `package.json`, no hidden globals). The clearest fix is switching from barrel/default imports to precise imports.
Before
After
CSS does not get ES-module tree shaking like JS, but PostCSS / Tailwind / PurgeCSS serve the same purpose: strip unused rules before you run an online CSS minifier or cssnano at the end.
Measure what actually moves in your bundle
You read each technique in different columns of the report: tree shaking reduces module count and surface area (e.g., lodash nearly disappears), minification trims gzip size for the same graph. Use a treemap to catch regressions after dependency bumps.

Always build with `NODE_ENV=production` (or equivalent): without it some bundlers skip optimizations and dead-code elimination.
Side-by-side mental model
Use this when sizing optimization tickets: first ask whether code should exist in the graph at all; second ask how to shrink its textual encoding.
Minification
Tree shaking
Prevent paper optimizations.
Minify a standalone file fast
When Webpack/Vite is not wired yet — or you ship a lone script — paste JS into our client-side tool for compact output with an inverse Beautify path.
Paste or load your file
Use the online JavaScript minifier; nothing leaves your browser.
Toggle Beautify
Swap to Beautify for readable formatting — handy before copying back into your toolchain.
Do not confuse with shaking
The tool minifies one file at a time; dropping unused modules requires a production bundler graph.
As codebases grow, align with our online minifiers vs build tools guide — browsers stay perfect for exceptions and spikes.
Combine tree shaking and minification in builds
`vite build` runs Rollup in production with tree shaking and esbuild minification by default — ensure imports stay ES-module friendly.
Configuration
Usage
`mode: 'production'` enables usedExports plus minification; verify upstream libraries declare sideEffects accurately.
Configuration
Usage
Chain Tailwind/PurgeCSS then minification — see our CSS minification guide. Logical shaking happens during purge; minification polishes bytes afterward.
Basic example
File minification
Conclusion
Tree shaking and minification complement each other: one trims the dependency graph, the other encodes what remains with fewer bytes. Skip either and you either ship dead modules or verbose output for an already slim graph. Online minifiers cover snippets outside the repo; automate bundlers for everything else while keeping FastMinify handy for edge cases.
Related Articles

Lower your OpenAI/Anthropic/Gemini bill: estimate input/output, model batch and caching in your calculations — verified rates, 100% local.

RAG, multi-turn agents, system prompts: calculate context-window usage and remaining headroom before calling the API.

Honest guide: uniform tabular data, convert → count → price → context workflow; not a JSON/YAML replacement manifesto.