Web & APIs · 7 min read
Minification vs Compression
Minification and compression both make web assets smaller, and they are often confused or assumed to be alternatives. They are complementary. Minification rewrites the code itself so it contains fewer characters; compression encodes the bytes for transfer so they take fewer bytes on the network, and the browser decodes them back exactly. A well-optimized site does both.
This guide explains what each technique does, how much each saves, how they interact, and the settings worth checking on your own site.
Try it yourself with the related tool.
Beautify or minify code →Advertisement
What minification does
A minifier removes everything the browser does not need to run or render the code: comments, whitespace, line breaks and optional semicolons. JavaScript minifiers such as Terser, esbuild and SWC go further, renaming local variables to single letters, removing dead code and simplifying expressions — if (x) { return true } else { return false } can become return!!x. CSS minifiers shorten colours (#ffffff to #fff), merge rules and drop redundant units. HTML minifiers collapse whitespace and remove optional tags and attribute quotes.
The result is permanently different text. It runs the same, but it is hard for people to read, which is why minified code ships with source maps that let browser developer tools show the original.
What compression does
Compression is applied by the server or CDN when sending the response, using an algorithm the browser advertised in its Accept-Encoding header. Gzip has been universal for decades. Brotli, supported by all modern browsers over HTTPS, typically produces files 15 to 25 percent smaller than gzip for text. Zstandard is a newer option gaining browser support. The browser decompresses the bytes and gets back exactly the original file — compression is lossless.
Compression works by finding repeated sequences and replacing them with short references, so it is extremely effective on text, where variable names, tags and keywords repeat constantly.
How much each saves
For a typical JavaScript bundle, minification alone might cut size by 40 to 60 percent. Compression alone often cuts 70 to 80 percent. Together they compound: a 500 KB unminified library might become 200 KB minified and around 60 KB after Brotli. Because compression already removes much of the redundancy that whitespace and long names create, the gain from minification on top of compression is smaller than either number suggests — but it is still real, and minification also reduces the time the browser spends parsing.
What not to compress
Images in JPEG, PNG, WebP and AVIF, video, audio, fonts in WOFF2, and ZIP files are already compressed. Running gzip on them wastes CPU and can even make them slightly larger. Servers should compress text types — HTML, CSS, JavaScript, JSON, SVG, XML and plain text — and skip the rest. Very small responses, under about one kilobyte, gain little from compression.
Static versus on-the-fly compression
A server can compress each response as it is sent, or serve files compressed ahead of time during the build. Pre-compressing lets you use the highest, slowest Brotli setting once, rather than a faster, weaker setting on every request. Most CDNs and hosting platforms handle compression automatically, but it is worth confirming: open the network panel in browser developer tools and check the Content-Encoding response header for br or gzip.
Security note on compressing secrets
Compressing responses that contain both a secret, such as a CSRF token, and attacker-controlled input can leak the secret through response sizes; this is the basis of the BREACH attack. Common mitigations are to avoid reflecting user input in pages that contain secrets, or to mask tokens so they differ on every response.
Beyond both
The smallest file is the one never sent. Tree-shaking removes unused exports from bundles; code splitting loads code only for the route being viewed; caching with long-lived, content-hashed file names means returning visitors download nothing at all. Audit bundles with a visualizer to find large dependencies — swapping one heavy library for a lighter one often saves more than every minifier setting combined.

