Data compression is encoding information in fewer bits than the original takes. A program called an encoder shrinks the data; a decoder turns it back into something usable. Compression works because most data repeats itself: the same words in a text file, the same colors across an image. The encoder stores those patterns once and points back to them.
The trade is time for space. Compressing and decompressing costs a little processing, and in return files take less storage and less time to send. On the web that trade almost always pays off.
Lossless or lossy compression
There are two kinds, and the difference decides where you can use each:
| Lossless | Lossy | |
|---|---|---|
| What you get back | Exactly the original, byte for byte | A close copy; some detail is gone for good |
| Use it for | Text, code, data, logos and screenshots | Photos, audio and video |
| Examples | gzip, Brotli, ZIP, PNG, GIF | JPEG, most video formats |
Some formats do both. WebP images can be lossless or lossy, and Google says they come out 26% smaller than PNG and 25–34% smaller than comparable JPEGs (Google for Developers). See image compression and WebP.
How compression works on the web
Web pages use compression at two levels.
Inside the file format. Images, video and audio are already compressed when you save them. Choosing the right format and quality is how you make them smaller.
During the HTTP transfer. Text files such as HTML, CSS, JavaScript, JSON and SVG are compressed by the server as it sends them. The browser lists the methods it understands in the Accept-Encoding request header. The server picks one, compresses the response and names its choice in Content-Encoding:
Request: Accept-Encoding: gzip, deflate, br, zstd
Response: Content-Encoding: br
Vary: Accept-Encoding
The Vary: Accept-Encoding header tells caches to store the compressed and uncompressed versions separately.
The methods you'll meet are gzip, the most common; Brotli (br), which usually compresses text further; and Zstandard (zstd). For some documents, HTTP compression cuts the size by up to 70% (MDN).
Don't compress files that are already compressed, such as JPEGs, videos or ZIP files. A second pass gains nothing and can make them bigger.
Why it matters for speed and search
Smaller files reach the browser sooner, so the page can start showing content earlier. That helps loading measures such as Largest Contentful Paint, especially on slow mobile connections.
Search engine crawlers benefit too. Google's crawlers accept gzip, deflate and Brotli. One limit to know: when Googlebot crawls for Google Search, it reads only the first 2 MB of a file (64 MB of a PDF), and Google measures that on the uncompressed data. Compression makes the download faster, but it won't let Google read a page that's too long.
How to check whether your site compresses
In Chrome DevTools, open the Network panel, click the HTML document and look for content-encoding under Response Headers. With large request rows switched on, the Size column shows the transferred (compressed) size above the full size.
From the command line, ask for compression and read the answer:
curl -s -o /dev/null -D - -H "Accept-Encoding: br, gzip" https://example.com | grep -i content-encoding
No content-encoding line means the response came back uncompressed.
In Lighthouse, since version 13 the check sits in the "Document request latency" insight, which flags an HTML document sent uncompressed.
What to compress on your site
- Turn on gzip or Brotli on your server or CDN for HTML, CSS, JavaScript, JSON, SVG and XML.
- Leave files that are already compressed, such as JPEG, WebP, MP4 and ZIP, alone at the HTTP level.
- Shrink images by choosing the format and quality before you upload them.
- Remove what the page doesn't need first. Compression makes files smaller; it doesn't make unused code free to parse and run.