Compression — Performance

Compression shrinks text files before they're sent. The browser offers through Accept-Encoding (Chrome over HTTPS: gzip, deflate, br, zstd), the server…

The Lab's app.js is 199.9 KB. On a 1.6 Mbps connection, that file takes about 1 second just to travel. With a little server setup, only 37.7 KB travels, without changing a single character of the code.

Compression means the server shrinks text files (HTML, CSS, JavaScript, JSON, SVG) before sending them, and the browser puts them back to full size as soon as they arrive. The content is exactly the same, only far less goes over the network. It's like packing clothes in a vacuum bag: the same clothes, a much more compact suitcase, and once opened at the destination they're back to normal size. But you can't vacuum a brick: files that are already compressed, like photos and video, can't be shrunk again.

Browser and server agree through headers

The browser says which compressions it can open with the request header Accept-Encoding. The server picks one, compresses, and names its choice in the response header Content-Encoding:

GET /assets/app.js HTTP/2
accept-encoding: gzip, deflate, br, zstd

HTTP/2 200
content-type: text/javascript
content-encoding: br
vary: Accept-Encoding
content-length: 37673

You don't write any code in the page: the browser opens the compression automatically. What you set up is the server.

How much smaller?

These are the files in the Lab, really measured with gzip level 6 and Brotli level 11 (1 KB = 1,000 bytes):

File Original gzip Brotli
app.js 199.9 KB 46.7 KB (76.6% smaller) 37.7 KB (81.1% smaller)
style.css 47.3 KB 8.9 KB (81.2%) 7.9 KB (83.4%)
index.html 49.4 KB 14.0 KB (71.6%) 11.8 KB (76.1%)
hero.webp 98.9 KB 98.5 KB (0.4%) 98.3 KB (0.6%)

app.js becomes 46.7 KB with gzip. Text compresses easily because it's full of repetition: function, const, class names, the same HTML tags over and over. In general, text files shrink by 60 to 80 percent, about 70 percent on average; JavaScript that's already minified often shrinks by about 70 to 80 percent.

On a 1.6 Mbps network (about 200 KB per second), app.js takes 1 second sent as is, and only 0.19 seconds with Brotli. One thing doesn't change: once it arrives, the browser opens the compression, so the JavaScript engine still parses and runs 199.9 KB. Compression saves network, not CPU work on the user's phone.

gzip or Brotli

Static or on-the-fly

There are two moments to compress:

  1. Static, at build time. Files whose content doesn't change (app.js, style.css) are compressed once at deploy, saved as app.js.br and app.js.gz, and the server just sends the matching one. Because it happens once, use the highest level: Brotli 11.
  2. On-the-fly, every request. Responses made on the spot, for example JSON from an API, are compressed every time they're sent. Use a medium level, for example Brotli 4 to 5 or gzip 6, so the server doesn't get busy compressing.

An example in Nginx (the Brotli module is installed separately):

gzip on;
gzip_types text/css text/javascript application/javascript application/json image/svg+xml;
gzip_min_length 1000;   # files under 1,000 bytes are not compressed
gzip_static on;         # send app.js.gz if it already exists

brotli on;
brotli_comp_level 5;    # on-the-fly: a medium level
brotli_types text/css text/javascript application/javascript application/json image/svg+xml;
brotli_static on;       # send the app.js.br from the build if it exists

text/html doesn't need to be listed: Nginx always compresses HTML once gzip on is set. Many modern hosts (Cloudflare, Vercel, Netlify) already compress automatically.

What doesn't need compressing

🎯 Check it in DevTools

Open the Network tab, reload the page, then click a JS file. Under Response Headers, look for content-encoding: it should be br or gzip. In the file list, the Size column shows two numbers if you turn on "Big request rows": the top one is the size that crossed the network, the bottom one the original size. If the two are almost the same for a text file, compression isn't on yet.

⚠️ What often goes wrong when setting up compression

TL;DR: Compression shrinks text files before they're sent. The browser offers through Accept-Encoding (Chrome over HTTPS: gzip, deflate, br, zstd), the server answers with Content-Encoding and Vary: Accept-Encoding. Text usually shrinks by 60 to 80 percent, about 70 percent on average: in the Lab app.js goes from 199.9 KB to 46.7 KB (gzip) or 37.7 KB (Brotli). Brotli is about 10 to 25 percent smaller than gzip but much slower to compress at level 11, so that level is used at build time; dynamic responses are compressed on-the-fly at a medium level. Don't compress images, video, WOFF2 or files under about 1 KB. Compression saves network, not parse work.

⚡ Agreeing through headers

Write:

accept-encoding: gzip, deflate, br, zstd
content-encoding: br
vary: Accept-Encoding

Meaning: The browser offers the compressions it can open through Accept-Encoding, and the server names its choice in Content-Encoding. You write no code in the page: what you set up is the server.

⚡ Text shrinks a lot

Meaning: Text is full of repetition, so it usually shrinks by 60 to 80 percent. In the Lab, app.js goes from 199.9 KB to 46.7 KB with gzip. But the browser still parses 199.9 KB: compression saves network, not CPU work.

⚡ gzip or Brotli

Guess first: what is the most important difference between gzip and Brotli?

Brotli's output is about 10 to 25 percent smaller, but it is much slower to compress at level 11. That's why level 11 is used at build time, and dynamic responses are compressed at a medium level.

Meaning: Images, video, WOFF2 and files under about 1 KB don't need compressing: they save next to nothing, and can even grow.

What you will learn