HomeBlogGzip vs Brotli vs LZ4: Measured Comparison

Gzip vs Brotli vs LZ4: Measured Comparison

Measured by runtime: Brotli-11 wins on size but crawls at 0.4 MB/s, LZ4's speed is a WASM-vs-JS question, and a 99-byte JSON grows when gzipped.

DevKit is maintained by hcatree. I wrote the tools on this site and ran the compression benchmarks myself — the script and the raw data download from the benchmarks page.

2026-11-1313 min

Every compression comparison says the same three things: gzip is the safe default, Brotli compresses better, LZ4 is the fast one. All three are true in C on a Linux server. Two of them stopped being true when I measured them in JavaScript.

This article reports numbers I measured on this machine — six payloads, sixteen codec settings across four runtime classes, compression and decompression throughput, all round-trip verified. It also explains why the numbers look the way they do, from the format specifications rather than from folklore.

Three results contradicted what I expected. They are in sections 3, 4 and 5.

The short version

Question Answer from measurement
Smallest output on structured JSON? Brotli-11: 27,870 B where gzip-6 gave 52,145 B — 46.6% smaller
What does that cost? 0.4 MB/s compression, 224× slower than gzip-6
Is Brotli-11 safe per request? No. Precompress static assets; use gzip-1 or Brotli-4 dynamically
Is LZ4 the fast option in JavaScript? It depends on the port. lz4js (pure-JS) did 148 / 158 MB/s — slower than native gzip. lz4-wasm (WASM) did 342 / 523 MB/s — the fastest decompressor here
Does compression always shrink data? No. 99 B of JSON became 108 B with gzip; random bytes grew by 83 B
Best default for a website? Brotli-11 at build time for static files, gzip for dynamic responses

Compressed size across six payloads and six codec settings

1. What each format actually is

The size and speed differences are consequences of three design decisions. Knowing them lets you predict the results instead of memorizing them.

Gzip is a container around DEFLATE. RFC 1952 calls it a "lossless compressed data format" and defines the wrapper: a header starting with the magic bytes ID1 = 31, ID2 = 139 (1f 8b), and a trailer holding a CRC32 plus ISIZE, the input size modulo 2^32. The payload is DEFLATE, specified separately in RFC 1951:

"Each block is compressed using a combination of the LZ77 algorithm and Huffman coding."

"The LZ77 algorithm may use a reference to a duplicated string occurring in a previous block, up to 32K input bytes before."

That 32 KB window is gzip's ceiling on how far back it can look for a repeat. The fixed header and trailer cost 18 bytes before any data is encoded — remember that for section 5.

Brotli is LZ77 plus Huffman plus two extras. RFC 7932 describes it as "a combination of the LZ77 algorithm and Huffman coding", so the family resemblance is real. The differences:

  • A much larger window: "The sliding window size is a power of two, minus 16, where the power is in the range of 10 to 24" — up to roughly 16 MB, against gzip's 32 KB.
  • Context modeling for literals: "The context for encoding the next literal is defined by the last two bytes in the stream". The probability model adapts to what kind of text it is inside.
  • A static dictionary defined in the RFC itself (§8), so common words and phrases can be referenced without having appeared earlier in the input.

The dictionary is why Brotli wins on short text where gzip has nothing to reference yet. The larger window and context model are why it wins on long text too.

LZ4 deliberately skips the second half. The block format specification is blunt:

"There is no entropy encoder back-end"

A block is a series of sequences. Each starts with a token containing "two 4-bits fields" — literal length and match length — followed by the literals themselves, then a match offset which "is a 2 bytes value". The minimum match length ("minmatch") "is 4".

Two consequences follow directly. A 2-byte offset caps the look-back at 64 KB. And with no Huffman or arithmetic coding stage, the output is byte-aligned and the decoder is essentially a copy loop — which is where LZ4's famous speed comes from, and also why its ratio is worse.

gzip (DEFLATE) Brotli LZ4
Specification RFC 1952 + RFC 1951 RFC 7932 LZ4 block/frame format
Look-back window 32 KB up to ~16 MB 64 KB
Entropy coding Huffman Huffman + context models none
Built-in dictionary no yes no
Integrity check CRC32 in trailer none in the format optional xxHash-32
Design priority balance ratio speed

One row there has operational consequences: Brotli defines no checksum, so a corrupted body can decode into plausible garbage instead of failing loudly. Gzip's CRC32 catches that. If you transport Brotli over a channel that can corrupt bytes, add your own integrity check.

2. Measured: six payloads, sixteen implementations

Method. Node.js v24.22.0 on Windows x64. Every codec is tagged with its runtime, because that is the whole lesson: native-C (Node's zlib), pure-JS (pako 3.0.1, fflate 0.8.3, lz4js 0.2.0), WASM (brotli-wasm 3.0.1, lz4-wasm 0.9.2), and the browser's native CompressionStream API. Timings are the median of 15 rounds after 2 warmups — 5 rounds for the two slow Brotli settings, which run at a fraction of a MB/s. Every output was decompressed and compared byte-for-byte against the input; all 96 codec/payload combinations round-tripped. Byte counts reproduce exactly run-to-run; the full per-runtime dataset and the script that produced it are published at /benchmarks.

The six payloads, chosen to cover the cases that actually differ:

  • orders JSON — 553,706 B. 1,000 nested API records: repeated field names, enums, numbers, base64.
  • log lines — 503,353 B. Timestamped text lines with a repeated structure.
  • real JS chunk — 229,820 B. An actual production bundle from this site's build output, not a synthetic sample.
  • random bytes — 204,800 B. Incompressible by construction.
  • tiny JSON — 99 B. A single small object, the size of a real API heartbeat response.
  • high-repeat text — 180,000 B. One sentence repeated 4,000 times, the case where entropy coders run away with it.

Compressed size as a percentage of the original:

Payload gzip-1 gzip-6 gzip-9 Brotli-4 Brotli-11 LZ4
orders JSON (553,706 B) 11.9% 9.4% 8.6% 7.9% 5.0% 17.3%
log lines (503,353 B) 16.0% 13.7% 11.6% 13.0% 9.2% 23.1%
real JS chunk (229,820 B) 35.6% 31.3% 31.2% 31.2% 26.7% 48.0%
random bytes (204,800 B) 100.0% 100.0% 100.0% 100.0% 100.0% 100.0%
tiny JSON (99 B) 109.1% 109.1% 109.1% 84.8% 89.9% 115.2%
high-repeat text (180,000 B) 0.63% 0.34% 0.34% 0.03% 0.03% 0.43%

Absolute bytes for the two most useful rows:

Payload gzip-6 gzip-9 Brotli-4 Brotli-11 LZ4
orders JSON 52,145 B 47,554 B 43,625 B 27,870 B 95,766 B
real JS chunk 71,969 B 71,809 B 71,752 B 61,420 B 110,360 B

Three things in that table are worth more than the headline.

gzip-9 is almost pointless on a JS bundle. It saved 160 bytes over gzip-6 (71,809 vs 71,969) — 0.2% — while running at 20.4 MB/s instead of 28.9 MB/s. If you are choosing a gzip level for web assets, level 6 is where the curve flattens.

Brotli-4 matched gzip-9's size at a higher speed. 71,752 B vs 71,809 B, at 40.2 MB/s vs 20.4 MB/s. On log lines it was strictly better than gzip-6 on both axes: 65,509 B vs 68,967 B, and 79.4 MB/s vs 57.1 MB/s. This is the setting most people never try, because the conversation is always "gzip or Brotli-11".

LZ4's ratio penalty is consistent and structural. 1.84× gzip-6's size on orders JSON, 1.68× on logs, 1.53× on the JS chunk. That is the cost of having no entropy coder, and unlike the speed numbers it transfers across languages — a native LZ4 will also be roughly this much larger.

3. The Brotli-11 speed trap

Here is the first result that changed my behavior. Throughput on orders JSON, in MB/s of input processed:

Setting Compress Decompress
gzip-1 213.8 316.8
gzip-6 89.8 251.9
gzip-9 37.2 383.0
Brotli-4 106.6 328.1
Brotli-11 0.4 289.7
LZ4 (lz4js) 148.3 157.5

Compression and decompression throughput for six codec settings

Brotli-11 compressed at 0.4 MB/s — 1,342 ms for this one payload. That is 224× slower than gzip-6 (89.8 MB/s) and 267× slower than Brotli-4 (106.6 MB/s), the same codec at a different quality setting. Per request. On one core.

What it bought: 27,870 B instead of Brotli-4's 43,625 B, a 36.1% reduction. Real, but not something you can afford inside a request handler.

Now look at the decompression column. Brotli-11 decompressed at 289.7 MB/s — faster than gzip-6 in the same run, and the same order of magnitude as everything else. The asymmetry is the whole story:

Brotli-11 is expensive to produce and cheap to consume.

That makes it a build-time tool, not a request-time tool. Compress your static assets once at quality 11, serve the precompressed bytes forever, and every visitor gets the 26.7% ratio with zero server CPU. Compress a dynamic API response at quality 11 and you have turned a 6 ms handler into a 1.3 s handler.

Two supporting details from the same run. On random bytes Brotli-11 ran at 1.8 MB/s rather than 0.4 — it detects incompressible input and gives up early, so the worst case is bounded. And on the 99-byte payload it ran at 0.1 MB/s, which is a per-call overhead artifact, not a throughput figure; at that size the absolute time is microseconds either way.

4. LZ4: the number you read is not the number you get

The second surprise. The official LZ4 project publishes benchmark results from lzbench on the Silesia corpus, single-threaded, on a Core i7-9700K under Ubuntu 24.04 with GCC 13.3.0:

Codec (native C) Ratio Compress Decompress
LZ4 1.11.0, default 2.112 770 MB/s 4950 MB/s
LZ4 HC -9 2.717 40 MB/s 4800 MB/s
zlib 1.3.2 -6 3.106 36 MB/s 430 MB/s
Zstandard 1.5.7 -1 2.896 510 MB/s 1570 MB/s

Those are the numbers behind "LZ4 decompresses at multiple GB/s". They are correct, and they are measured in C.

My measurement, same algorithm, JavaScript implementation:

Native C (official) lz4js 0.2.0 (measured) Gap
Compress 770 MB/s 148.3 MB/s 5.2×
Decompress 4950 MB/s 157.5 MB/s 31.4×

And the part that actually matters: in the same JavaScript runtime, gzip-6 decompressed at 251.9 MB/s while LZ4 decompressed at 157.5 MB/s. The "fast" codec was 1.6× slower than the "slow" one, because Node's zlib is native C and lz4js is a JavaScript byte loop.

lz4js describes itself as "An Lz4 implementation for the browser" and has no dependencies — no WASM, no native addon. Native LZ4's decompression speed comes from tight copy loops that a compiler vectorizes; a bounds-checked JavaScript loop over a Uint8Array does not get that treatment.

The rule this generalizes to: a codec's speed is a property of an implementation, not of a format. The ratio is largely a property of the format (section 2 showed LZ4's penalty is structural). The speed is not. Before you pick LZ4 for a Node service or a browser tool, benchmark the library you will actually ship. If you are writing C, Rust or Go, the official numbers are the relevant ones and LZ4 is a genuinely excellent choice.

Then I gave LZ4 a WASM implementation, and the conclusion flipped

The "slow LZ4" above was never really LZ4 — it was one hand-written JavaScript loop. lz4-wasm 0.9.2 compiles the same LZ4 C library to WebAssembly. Same data, same machine, same browser-runtime class as lz4js:

LZ4 implementation runtime format Compress Decompress Output (orders JSON)
lz4js 0.2.0 pure-JS frame 148.3 MB/s 157.5 MB/s 95,766 B
lz4-wasm 0.9.2 WASM block 341.7 MB/s 523.2 MB/s 97,646 B
(reference) gzip-6 native-C — 89.8 MB/s 251.9 MB/s 52,145 B

Hand LZ4 to WebAssembly and it decompresses at 523.2 MB/s — faster than any native-C gzip setting on the same machine, and 3.3× its own pure-JS self. That reclaims, in the browser, the reputation the official C numbers promised.

LZ4 decompression throughput by implementation runtime

Two honest asterisks on that WASM row. First, it writes the block format, not the frame format: no magic header, no per-block checksum, and the decoder must be told the original size up front (lz4js emits self-describing frames). That is also why its output here is slightly larger than the pure-JS frame version despite being the C library — the two parsers disagree, a reminder that even the ratio is implementation-dependent. Second, the port is from 2022 and lightly maintained, and that is itself the finding: brotli and zstd have healthy browser ports; LZ4 does not. If you choose a codec partly because you want a maintained WASM library, LZ4's ecosystem is the weakest of the three.

And what a browser gives you with no library at all. The Web CompressionStream / DecompressionStream API is native and dependency-free, and its format list is fixed by the spec's own enum — brotli, deflate, deflate-raw, gzip, and no zstd. Whether you can call the ones in that enum is a second, separate question: in the Chromium I measured (150.0.7871.114) both brotli and zstd throw Unsupported compression format, while MDN's compatibility data gives brotli in Firefox 147 and Safari 18.4. On the Node build this benchmark ran on, brotli constructs and round-trips and zstd is the only format the constructor refuses. So "the platform ships me gzip and deflate" is true of the browser you are most likely serving, and the full story — including why zstd is a wire format and not an API — is in Zstd vs Brotli: 19 Codecs, Real Numbers. On orders JSON gzip through that API measured 59.2 MB/s compress / 104.6 MB/s decompress. Against pure-JS pako (26 / 114.6) it is 2.3× faster at the end you pay per request and about even at the end you pay per read. Brotli and LZ4 in the browser still mean shipping WASM. That is the decision the headline size tables quietly hide: which codec can I actually load, in which form, and how fast is that specific build.

The deflate rows make the implementation point sharper than any prose can. Native zlib, pako and the browser's CompressionStream all emit exactly 52,133 bytes for the same input at level 6 — bit-for-bit identical output — at 84.9, 25.7 and 68.8 MB/s. Same format, same level, same bytes out. The only variable is the code that produces them.

One honest caveat about my own setup: the gzip figures here come from Node's zlib, while the Gzip / LZ4 tool on this site uses pako for gzip in the browser. Both are DEFLATE, so the ratios are close — measured at 52,145 B for both on orders JSON — but throughput is not identical, and I now have pako in the table (26 / 114.6 MB/s) to show the gap rather than assert it. For LZ4 I measured both the lz4js library the tool actually ships and the WASM port, so the browser-relevant numbers are observed, not extrapolated. Every byte count here is reproduced exactly by the dataset behind /benchmarks; the MB/s figures are this article's own run, which that page does not publish.

5. When compression makes things bigger

The third surprise, and the one most likely to cost you money in production.

Small payloads. The 99-byte JSON object:

Setting Output Change
gzip (any level) 108 B +9 B (+9.1%)
LZ4 frame 114 B +15 B (+15.2%)
Brotli-11 89 B −10 B (−10.1%)
Brotli-4 84 B −15 B (−15.2%)

Gzip's 18 bytes of fixed header and trailer are most of the loss. There is no repetition in 99 bytes for LZ77 to exploit, so the compressed body barely shrinks and the wrapper pushes the total over the original. Brotli's smaller header and static dictionary let it win even here — but by 15 bytes, which is not worth a CPU cycle.

This is why servers have a minimum-length threshold (nginx's gzip_min_length defaults to 20 bytes, and Cloudflare will not compress a response under 48 bytes for gzip or 50 for Brotli and Zstandard). Set one. And if you then Base64-encode the compressed bytes for transport — as JSON APIs and browser tools often do — you add 33% on top, which erases the entire gain at this size.

Incompressible payloads. 204,800 bytes of random data:

Setting Output Overhead
gzip (any level) 204,883 B +83 B
Brotli (4 or 11) 204,805 B +5 B
LZ4 frame 204,815 B +15 B

Every codec detected the entropy and stored the data essentially verbatim, adding only framing. Real-world equivalents: JPEG, PNG, MP4, ZIP, and anything encrypted. Compressing them burns CPU to add bytes.

The practical version of both results is a two-line policy: skip compression below ~1 KB, and skip it for content types that are already compressed. Everything else is a ratio question.

This section is the short version of a much larger measurement. What each container charges for an empty input, the exact byte size at which each codec stops losing money, why gzip's overhead grows with the input instead of staying at 20 bytes, and 801 round-tripped runs covering all of it, are in When Compression Makes Your Data Bigger.

6. Choosing

Your situation Pick Measured justification
Static JS / CSS / HTML, built ahead of time Brotli-11, gzip-9 fallback 26.7% vs 31.3% on a real bundle; decompresses at 212.6 MB/s
Dynamic HTML or JSON, per request gzip-1 or Brotli-4 213.8 and 106.6 MB/s; Brotli-11 would cost ~1.3 s per 553 KB
Dynamic text, some CPU headroom Brotli-4 Beat gzip-6 on size and speed on log lines
Logs / archives, written once, read rarely gzip-9 or Brotli-11 offline Ratio matters, latency does not
In-process cache, IPC, temp files, native code LZ4 4950 MB/s native decompression is in a different class
Browser, "I need it fast" LZ4 via lz4-wasm (523.2 MB/s decompress), or native CompressionStream gzip with no dependency pure-JS lz4js decompressed 3.3× slower than its own WASM build
Payload under ~1 KB Don't compress 99 B → 108 B with gzip
Images, video, archives, encrypted data Don't compress +83 B on 204,800 random bytes
Public web API, unknown clients gzip Universally supported; br is supported by all modern browsers

For HTTP, the client advertises what it accepts and the server picks one:

Accept-Encoding: gzip, deflate, br
Content-Encoding: br

If you serve different encodings from the same URL, send Vary: Accept-Encoding so caches do not hand a Brotli body to a client that only accepts gzip.

7. My reads

First, "which codec" is the wrong question — "which level" is the right one. Brotli-4 to Brotli-11 was a 267× speed difference for a 36% size difference on the same data. The gap between two settings of one codec was larger than the gap between codecs. Most teams pick a codec and never touch the level.

Second, most LZ4 advice is a C claim wearing a language-neutral costume — but the costume is fixable. The official 4950 MB/s is real, and lz4js in pure JavaScript is nowhere near it (148 MB/s). But lz4-wasm reaches 523 MB/s in the very same browser. So the honest statement is not "LZ4 is slow in JavaScript" — it is "a codec ships at the speed of a specific implementation, and you have to pick the right one." Any comparison that does not name the implementation is incomplete.

Third, the biggest available win is usually not a codec change — it is moving compression to build time. My real bundle went from 229,820 B to 61,420 B, a 73.3% reduction, at zero request-time CPU. That is available to any site with a build step, today, without changing a single line of application code.

Fourth, compression has a floor and it is above zero. Below roughly 100 bytes gzip loses. On high-entropy data every codec loses. A size threshold and a content-type allowlist are not optimizations, they are correctness.

Fifth, measure in the runtime you ship. Same format, same input, same machine: 5× to 31× differences depending on whether the implementation was native or JavaScript. The specifications tell you what the format can do; only a benchmark tells you what your library does.

8. Try it in the browser

The Gzip / LZ4 tool on this site compresses and decompresses UTF-8 text or files (up to 20 MB) entirely locally, shows the before-and-after byte counts, and downloads the result as .gz or .lz4. Decompressed output downloads as .bin, because the original filename is not recoverable from the bytes.

Two things to try against the claims above:

  • Paste a short JSON object and compress it with gzip. If it is under ~100 bytes you will see the output get larger, exactly as in section 5.
  • Compress the same text with both algorithms and compare the byte counts. Expect LZ4 to land around 1.5–1.8× gzip's size.

Text mode returns Base64, which inflates the result by about 33% — compare the byte counts the tool reports, not the length of the Base64 string. File mode gives you the raw binary.


References:

Benchmark environment: Node.js v24.22.0, Windows x64. Codecs are grouped by runtime — native-C (Node zlib), pure-JS (pako 3.0.1, fflate 0.8.3, lz4js 0.2.0), WASM (brotli-wasm 3.0.1, lz4-wasm 0.9.2) and the browser's CompressionStream. 2 warmup rounds then the median of 15 timed rounds (5 for the two slow Brotli settings); all 96 codec/payload combinations round-trip verified. Byte counts were identical across independent runs; throughput is not, and the drift is measured rather than asserted: two consecutive runs of this same script on the published 114-row dataset moved the median MB/s figure by 25.2%, moved 127 of the 227 figures by more than 20%, and moved the worst single figure by 347% — the row that decompresses the real JS chunk through CompressionStream gzip read 12.7 MB/s in one run and 56.8 in the next. Treat MB/s as indicative and the bytes as exact. The canonical dataset now published at /benchmarks is a later re-freeze by this same script with zstd added — its original 96 rows are byte-identical to every figure in this article, its MB/s column is a different sample, and the drift numbers above come from a pair of runs taken off that published dataset on 2026-11-19, which reproduced all 114 of its byte counts exactly. Native LZ4 figures in section 4 are quoted from the official project's lzbench results (Silesia corpus, single-thread, Core i7-9700K, Ubuntu 24.04, GCC 13.3.0), not measured here. Measured 2026-11-15.

Discuss this articleEchoForum · no sign-up, replies in any language

Related Tools

Gzip / LZ4Gunzip Base64 or hex in one step