Skip to content
Formatters 7 min read

JavaScript & CSS Minification: How It Works and Why It Matters

Understand what minification does to your JS and CSS, how tree-shaking and dead-code elimination work, the difference between minification and compression, and how to measure the gains.

ToolsVito Team

What Minification Does

Minification transforms source code into a functionally identical but smaller representation. For JavaScript, this includes:

  • Removing whitespace, newlines, and comments
  • Shortening variable and function names (userAuthenticationa)
  • Collapsing constant expressions (60 * 60 * 2486400)
  • Removing unreachable code and unused variables
  • Inlining small functions

For CSS: removing whitespace, comments, shortening color values (#ffffff#fff), merging duplicate rules, and removing zero units (0px0).

Minification vs Compression

These are two separate, complementary steps:

  • Minification: Reduces the source itself. The minified file is served to anyone, compressed or not.
  • Compression (gzip/Brotli): Applied at the HTTP transport layer. Brotli typically achieves 15–25% better compression than gzip.

Always do both. A minified + Brotli-compressed file is typically 75–85% smaller than the original source.

Typical Size Reductions

react.development.js  →  1,038 KB
react.production.min.js  →    11 KB  (minified, no dead code)
react.production.min.js.br  →  4 KB  (Brotli compressed)

bootstrap.css  →  232 KB
bootstrap.min.css  →  197 KB (minified only — CSS has less to gain)
bootstrap.min.css.br  →  24 KB (compressed)

Tree Shaking (Dead Code Elimination)

Tree shaking removes exports that are imported but never used. It requires ES modules (import/export) — CommonJS (require) is not statically analyzable.

// utils.js — exports 3 functions
export function used() { return "used"; }
export function alsoUsed() { return "also"; }
export function neverUsed() { return "dead"; }

// app.js — only imports two
import { used, alsoUsed } from "./utils.js";

// After tree shaking: neverUsed() is not in the bundle

Bundlers like Rollup, esbuild, and Vite do tree shaking automatically for ESM code.

Minifiers by Language

  • JavaScript: Terser (most widely used), esbuild (fastest), SWC (Rust-based, very fast)
  • CSS: cssnano, LightningCSS (Rust-based), clean-css
  • HTML: html-minifier-terser

Source Maps

Minified code is unreadable. Source maps link minified positions back to original source locations, letting browser DevTools show the original code during debugging:

# esbuild with source map
esbuild app.js --bundle --minify --sourcemap --outfile=dist/app.js
# Produces: dist/app.js + dist/app.js.map

# The minified file ends with:
//# sourceMappingURL=app.js.map

Never serve source maps to production users if your source contains secrets. Keep them on your error monitoring server (Sentry accepts source maps separately).

Measuring the Impact

# Check bundle sizes with bundlesize or size-limit
npm install --save-dev size-limit @size-limit/file

# package.json
{
  "size-limit": [
    { "path": "dist/app.js", "limit": "50 KB" }
  ]
}

Minify Code Online

Paste any JavaScript or CSS into ToolsVito's Minifier for instant minification with size reduction stats — no build tool setup required.

Try it now — free, runs in your browser

Minifier

Shrink code to its smallest size