Gzip Compression Algorithm
You'll learn how gzip compresses text-based web resources to dramatically reduce transfer sizes and speed up page loads.
What Gzip Does
Gzip is a compression algorithm that shrinks files before sending them over the network, then the browser decompresses them for use. Think of it like vacuum-sealing clothes before packing a suitcase—you fit far more into the same space, then unpack them at your destination.
Gzip works by finding repeated patterns in text and replacing them with shorter references. Since code, HTML, CSS, and JSON contain tons of repetition (keywords, tags, property names), gzip achieves impressive results.
Compression Ratios
Typical compression ratios for web resources:
- HTML/CSS/JavaScript: 70-80% reduction (a 100KB file becomes 20-30KB)
- JSON/XML: 60-70% reduction
- Already-compressed formats (JPEG, PNG, MP4): Little to no benefit—don't bother
The more repetitive the content, the better gzip performs.
Compatibility and Usage
Gzip is universally supported by all modern browsers (and has been for decades). The browser advertises support via the Accept-Encoding: gzip request header, and the server responds with Content-Encoding: gzip when compressed.
When to Use Gzip
Always use gzip for:
- HTML documents
- CSS stylesheets
- JavaScript files
- JSON/XML API responses
- SVG images (they're text!)
Skip gzip for:
- Images (JPEG, PNG, WebP—already compressed)
- Videos (MP4, WebM)
- Already-compressed files (ZIP, PDF)
Most web servers can enable gzip compression with simple configuration, and the CPU cost of compression/decompression is negligible compared to the bandwidth savings.
Key Takeaway: Gzip compresses text-based resources by 60-80%, is universally supported, and should be enabled for all textual web content (HTML, CSS, JS, JSON, SVG) but skipped for already-compressed binary formats.