Comparison

Minification vs Compression

Minification rewrites source to remove what a machine does not need — whitespace, comments, long names — and changes the file itself. Compression encodes the bytes for transfer with gzip or Brotli, and the browser decodes them back. They are not alternatives: compression saves far more, minification still helps, and shipping both is the normal answer.

JavaScript Minifier

Strip comments and whitespace from JavaScript safely.

Open the tool

These get discussed as if you pick one. You do not. They operate at different layers and their savings multiply: minification shrinks the source, compression then shrinks the shrunken source further.

What is worth knowing is the proportion. If you can only do one, enable compression — it is a server setting and typically saves far more than minifying ever will. Minification is worth doing as well, but it is the smaller half of the win.

What each one does

  • Minification changes the file. The result is what is stored and served, and it is what appears in your source map.
  • Compression changes only the transfer. The server encodes, the browser decodes, and the file on disk is unchanged.
  • Minification is a build step. Compression is a server or CDN setting.
  • Minification is irreversible in practice — original names and comments are gone. Compression is exactly reversible.

Why compression usually wins

Source code is extremely repetitive: the same keywords, property names and patterns over and over. Compression algorithms are built to exploit exactly that, so they typically remove the large majority of a text file's bytes. Minification removes whitespace, comments and — for JavaScript — shortens identifiers, but much of that whitespace was highly compressible anyway, so some of its saving overlaps with what compression would have achieved regardless. Brotli generally beats gzip on text and is supported by every current browser; serving Brotli with a gzip fallback is the standard arrangement.

Why minification is still worth doing

The savings stack, and the compressed size of a minified file is smaller than the compressed size of an unminified one. There are also gains compression cannot give you: shorter identifiers reduce the bytes the JavaScript parser must read, dead-code elimination removes work entirely rather than encoding it efficiently, and a smaller file decompresses faster on low-end devices. For JavaScript in particular, parse time matters as well as transfer.

Getting the order right

Minify at build time, compress at serve time. Compressing during the build and storing pre-compressed files is a valid optimisation — it lets the server use a slower, higher-quality compression setting once rather than on every request — but the sequence is always minify first, then compress. Compressing before minifying achieves nothing, since the minifier cannot read compressed bytes.

What not to minify

HTML gains little, because it is already mostly tags that compress extremely well, and aggressive HTML minification risks breaking pre, textarea and inline script content. JSON APIs should not be pretty-printed in the first place, so there is nothing to remove. Images and fonts are already compressed — running gzip over a PNG or a WOFF2 makes them slightly larger, which is why servers should be configured to compress text types only.

Frequently asked questions

If I enable gzip, do I still need to minify?
It is still worth doing. The savings stack, and minification gives you things compression cannot — shorter identifiers to parse and dead code removed rather than efficiently encoded. But if you can only do one, compression is the bigger win.
Brotli or gzip?
Brotli where available; it generally beats gzip on text and every current browser supports it. Keep gzip as the fallback — the client advertises what it accepts and the server picks, so serving both costs nothing.
Should I compress images?
No. PNG, JPEG, WebP, AVIF and WOFF2 are compressed formats already. Running gzip over them wastes CPU and usually makes the response slightly larger. Configure the server to compress text types only.
Does minified code run faster?
Slightly, and mostly at parse time rather than execution. A smaller file means fewer bytes for the JavaScript parser to read, which is noticeable on low-end mobile devices. Once parsed, minified and unminified code execute identically.
How do I debug minified code?
Source maps. The minifier emits a map from the minified output back to the original source, and browser developer tools use it to show you readable code. Serve maps only where you intend them to be available, since they expose your original source.

Tools used in this guide

All tools →
All guides →

Last reviewed .