Lossless Compression: Definition, Algorithms, Web Use

Lossless compression shrinks files without losing data — original is reconstructed exactly. Algorithms: gzip, Brotli, Zstd, LZ4, xz.

What is lossless compression?

Lossless compression is the practice of reducing file size while preserving every bit of the original data — decompression reconstructs the file exactly. The opposite is lossy compression (used for images, audio, video), which permanently discards data to achieve smaller sizes.

Lossless is the standard for any data where bit-perfect accuracy matters: text, source code, executables, databases, archive files, and HTTP responses (HTML, CSS, JavaScript, JSON, XML).

How lossless compression works

Lossless algorithms exploit redundancy in data — patterns that repeat or could be encoded more efficiently. Two main techniques:

  • Dictionary coding (LZ77, LZ78 family): replace repeated strings with references to a dictionary or earlier occurrence. Used by gzip, zlib, Zstd, LZ4.
  • Entropy coding (Huffman, arithmetic): assign shorter bit codes to more frequent symbols. Used as a final pass in most compressors.

Modern compressors (Brotli, Zstd) combine multiple techniques + pre-trained dictionaries.

Common lossless compression algorithms

AlgorithmSpeedCompression ratioBest for
gzip / DEFLATEFastModerateHTTP, tar.gz, zip — universal
BrotliMediumBetter than gzip (~20%)HTTP responses (modern web)
Zstandard (Zstd)Very fastBetter than gzipReal-time, log archives
LZ4FastestLower ratioReal-time streams, in-memory
xz / LZMASlowBest ratioSource archives, Linux distros
bzip2SlowBetter than gzipLegacy archives
SnappyVery fastLower ratioBigTable, RPC payloads

Lossless vs lossy compression

AspectLosslessLossy
ReconstructionBit-perfectApproximate
Use casesText, code, dataImages, audio, video
Examplesgzip, Brotli, PNG, FLACJPEG, MP3, H.264
Compression ratio2-10×10-100×
Reversible?YesNo

Lossless compression on the web

HTTP supports lossless compression via the Accept-Encoding request header and Content-Encoding response header:

GET / HTTP/1.1
Accept-Encoding: br, gzip, deflate

HTTP/1.1 200 OK
Content-Encoding: br
Content-Type: text/html
EncodingBrowser supportCompression ratio (HTML/JS)
gzipUniversal~70% reduction
Brotli~96% browsers~75% reduction
ZstdChrome 122+, growingSimilar to Brotli, faster

Use Brotli when available, fall back to gzip. Most CDNs (Cloudflare, Fastly, AWS CloudFront) negotiate this automatically.

When to use which compressor

HTTP responses (HTML, CSS, JS)

Brotli for static (compressed once at build, served from CDN). Brotli or gzip for dynamic. Compress at level 11 (max) for static; level 4-6 for dynamic.

Real-time streaming / low-latency

LZ4 or Snappy. Sacrifice ratio for speed. Used by Kafka, BigTable, Redis (with compression).

Long-term archives

xz/LZMA or zstd at high level. Best ratio matters more than speed.

Logs in motion

Zstd with shared dictionary trained on log format. Beats gzip at both speed + ratio.

Lossless compression best practices

  • Compress at build time when possible. Pre-compressed assets cost zero CPU at request time.
  • Use Brotli over gzip. ~20% smaller payloads; modern browser support.
  • Don't double-compress. Already-compressed files (JPEG, MP4, .zip) won't get smaller — wasted CPU.
  • Pick level for use case. Level 11 for build-time; level 4-6 for runtime.
  • Use shared dictionaries for similar payloads. Zstd shines here (e.g., compressing many similar JSON docs).
  • Measure both ratio and CPU. Higher levels = diminishing ratio gains but big CPU cost.
  • Compress before encryption. Encrypted data is essentially random — compresses poorly.
  • Cache compressed responses. Don't recompress identical responses on each request.

Compression in popular formats

FormatCompressionLossless?
PNGDEFLATEYes
WebPVP8/VP9Both modes (lossless or lossy)
FLACCustom (LPC + Rice)Yes (audio)
ZIPDEFLATE (typically)Yes
tar.gzgzipYes
ParquetSnappy/gzip/ZstdYes (data warehouse)

FAQ: lossless compression

What's the best lossless compressor?

Depends on tradeoff. Best ratio: xz/LZMA. Best balance: Zstd. Best speed: LZ4. Best web compatibility: gzip; modern web: Brotli.

Why is my file not getting smaller after compression?

Already-compressed data (JPEG, MP4, encrypted) has no exploitable redundancy. Random data is incompressible by definition.

Should I compress JSON?

Yes — JSON has high redundancy (repeated keys), often shrinks 80%+. Always Content-Encoding: gzip/br on JSON APIs.

What about images: PNG vs JPEG?

PNG is lossless (good for screenshots, logos, anything with sharp edges). JPEG is lossy (good for photos). WebP and AVIF support both.

Is Brotli better than gzip?

Yes — for HTTP responses, Brotli typically yields 15-25% smaller payloads at the same speed. Use Brotli; fall back to gzip for ancient clients.

Can compression help my website's performance?

Yes — compressed HTML/CSS/JS reduces transfer time, improving LCP and TTFB. Critical for Core Web Vitals.

What's the relationship between compression and encryption?

Compress first, then encrypt. Encryption produces high-entropy output that doesn't compress. Compressing encrypted data wastes CPU.

Test compressed payload performance with LoadFocus

LoadFocus runs JMeter and k6 scripts that measure response sizes and TTFB across 25+ regions, helping you verify Brotli/gzip configuration is actually saving bandwidth. Sign up free at loadfocus.com/signup.

How fast is your website?

Elevate its speed and SEO seamlessly with our Free Speed Test.

Free Website Speed Test

Analyze your website's load speed and improve its performance with our free page speed checker.

×