Web compression and bundle optimization explained
Understand gzip, Brotli, Zstandard, modern image formats, delivery hints and realistic bundle-size measurements.
Kareem Schultz
Technology enthusiast & systems builder ยท Guyana
Jump to a section
Compression has different jobs
HTML, CSS, JavaScript and JSON are usually good candidates for lossless HTTP compression. Gzip, Brotli and Zstandard encode repetitive bytes efficiently; a client negotiates supported content encodings through Accept-Encoding and receives a matching Content-Encoding response.
Images, video and archives are different: JPEG, AVIF, WebP and already-compressed formats often gain little from another lossless text compression pass. Measure actual bytes rather than assuming every gzip result represents a useful saving.
A measured example, not a universal benchmark
The interactive engineering lab compares three generated repetitive text fixtures. Both raw and compressed lengths are calculated at build time using Node zlib, not invented chart data. They are intentionally very compressible, and may be unrepresentative of minified production bundles.
Compression trade-offs
Compression ratio and CPU time are competing objectives. Brotli can perform especially well with static text assets; gzip is widely supported and may have lower encoding overhead for dynamic responses. Zstandard is also supported by modern delivery platforms, but browser/server negotiation and configuration matter. Avoid double-compressing and remember that transfer size is only one part of latency.
| Option | Often useful for | What to check |
|---|---|---|
| Gzip | Broad text asset compatibility | Quality level and CPU overhead |
| Brotli | Precompressed or cacheable web text | Negotiation and build-time effort |
| Zstandard | Modern negotiated content delivery | Client/CDN support and fallback |
| Modern image formats | Visual assets | Quality, responsiveness and decoding |
Other optimizations before adding more compression
Remove unneeded JavaScript, split code by real navigation boundaries, cache immutable hashed assets, load correct image dimensions and avoid rendering heavy graphs before they are needed. A smaller initial dependency graph may have greater user benefit than changing the compression algorithm alone.
- Inspect the production waterfall and transfer sizes
- Compare desktop and mobile under a representative connection
- Track JS parse/execute time in addition to download
- Cache immutable assets by content hash
- Use responsive images and avoid re-encoding already compressed assets
Validate the result at the edge
Cloudflare documents Gzip, Brotli and Zstandard support with configurable compression rules. Test real Accept-Encoding negotiation and ensure Vary behavior and cache policy are appropriate. Do not claim your site uses an encoding until the public response headers confirm it.
FAQ: does Brotli always win?
No. The best result depends on payload entropy, compression level, encode/decode cost and delivery path. For images and compressed archives a text codec may provide little benefit. A meaningful benchmark controls input content and records both CPU time and bytes on wire.
Frequently asked questions
Sources & further reading
These links provide the technical basis for the article. Check upstream advisories and documentation for revisions.