Skip to content
Kareem Schultz.
Web PerformanceGuide 12 min read

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

Conceptual illustrated cover for Compression examined
Original conceptual illustration; the article does not claim access to production systems.
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.

Measure one local file with gzip and Brotli
bash
wc -c example.jsongzip -6 -c example.json | wc -cbrotli -q 6 -c example.json | wc -c# Measure encoding time and client decode cost separately.

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.

OptionOften useful forWhat to check
GzipBroad text asset compatibilityQuality level and CPU overhead
BrotliPrecompressed or cacheable web textNegotiation and build-time effort
ZstandardModern negotiated content deliveryClient/CDN support and fallback
Modern image formatsVisual assetsQuality, 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.

Inspect HTTP content encoding
bash
curl -I -H "Accept-Encoding: gzip, br, zstd" https://example.com/# Look for Content-Encoding, Cache-Control and Vary.# Compare actual transferred bytes from a real network client.

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.

Read next

All articles