HomeBlogWhen Compression Makes Your Data Bigger

When Compression Makes Your Data Bigger

Measured over 540 runs: each container's byte floor, the size where gzip, brotli, zstd and LZ4 stop losing money, and why random data never shrinks.

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-1911 min read

There is a number most people carry for this: gzip costs about 20 bytes. It sounds like a rule and it is wrong in the two places where it matters. The floor is not 20 for every container — brotli's is 1 — and the floor is not the number that hurts: gzip charged 328 bytes of pure overhead for a megabyte of random data, on a run where it did nothing except fail to compress.

So I stopped quoting folklore and measured it. Nine codecs, thirty input sizes from 0 to 50,000 bytes, two kinds of input, plus a second sweep up to 1 MB and 54 rows on payloads people actually ship: 540 + 207 + 54 runs. Every run that had bytes in it was decompressed with the same library's own reader and compared byte for byte — 783 matches, zero mismatches, and the 18 zero-length inputs skipped because there is nothing to compare. The CSVs are at the bottom of this page, and every number in the article is in them.

What each container charges before it compresses a single byte

The floor is easy to see if you compress nothing at all. Here is the output for a zero-byte input and for a one-byte input (61, the letter a), from Node.js v24.22.0:

Codec Empty input One byte in Composition of the empty output
gzip (levels 1, 6, 9) 20 B 21 B 10 B header + 8 B trailer + 2 B empty block
LZ4 frame 11 B 16 B magic + frame descriptor + header checksum + end mark
zstd (levels 3, 19) 9 B 10 B magic + frame header + empty block
deflate (zlib, level 6) 8 B 9 B 2 B zlib header + 4 B Adler-32 + 2 B empty block
brotli (quality 4, 11) 1 B 5 B one byte: 3b

The bytes themselves, all of them real output from that run:

gzip, empty    1f8b 0800 00000000 04 0a  0300  00000000 00000000
               magic CM FLG MTIME        XFL OS  empty    CRC-32   ISIZE
                                                  block

deflate, empty 789c  0300  00000001
               hdr   block  Adler-32

zstd, empty    28b52ffd  20  00  010000
               magic     FHD win  block header

lz4-frame,     04224d18  40  70  df  00000000
empty          magic    FLG BD  HC   end mark

brotli, empty  3b

Two things in that dump are worth more than the table.

The gzip trailer is where the truth lives. The last eight bytes are CRC-32 and ISIZE — the checksum of the uncompressed data and its length, both zero here. ISIZE is the length of the original data modulo 2³², so a payload that is an exact multiple of 4 GiB writes zero into it, and a reader that trusts ISIZE rather than the stream length will believe the file was empty. That trailer is also why a truncated gzip stream is usually detectable rather than merely broken: the checksum and the length disagree, and you can say so.

MTIME is zero, so the output is reproducible. gzip stores no timestamp in this run, which is one reason the harness could compare two consecutive runs byte for byte. XFL is the only byte that leaks the level you asked for, and RFC 1952 defines just two of its values:

XFL = 2 - compressor used maximum compression, slowest algorithm XFL = 4 - compressor used fastest algorithm

Node writes 04 for levels 0 and 1, 02 for level 9, and 00 for levels 2 through 8. If you have ever tried to infer a gzip level from a header byte, that is the whole of the information available.

What the container costs, and what sets it

The floor is not the interesting number

A 20-byte floor on a 20-byte payload is a 100% tax, but nobody ships 20-byte payloads. What matters is the size at which each codec stops taking money out of your pocket — and that number is not the floor.

Brotli is the proof. Its floor is one byte, the cheapest container in the table, and yet brotli at quality 4 does not break even on real JSON until 60 bytes — sixty times its own floor. Conversely gzip has the highest floor in the table and breaks even at 160, only eight times its floor. The container cost amortises; the model cost is what sets the crossing point.

Where each codec stops losing money

The sweep used the first N bytes of a real order-feed JSON document — the same payload the compression benchmark runs on, so the two articles are not talking about different data. "Break-even" below is the first input size at which the output is not larger than the input; "first byte saved" is the first size at which it is strictly smaller.

Codec Break-even First byte actually saved
deflate-6 50 B (output = input) 100 B (−4)
brotli-11 50 B (output = input) 60 B (−5)
brotli-4 60 B (−3) 60 B (−3)
zstd-19 80 B (output = input) 100 B (−3)
zstd-3 100 B (−1) 100 B (−1)
gzip-1 160 B (−11) 160 B (−11)
gzip-6 / gzip-9 160 B (−12) 160 B (−12)
lz4-frame 300 B (output = input) 400 B (−57)

LZ4 is the odd one out and it is worth reading that row twice: it neither grows nor shrinks a 300-byte input, and then saves 57 bytes at 400. Its model needs a full match to lock onto before anything happens at all.

Counted the other way, as "how many of the nine codecs lose money at this size":

Input 45 B 50 B 60 B 80 B 100 B 160 B 250 B 300 B
Codecs that grow 9/9 7/9 6/9 5/9 4/9 1/9 1/9 0/9

Two readings of those rows that are easy to get wrong.

At 45 bytes, all nine lose. Not most. Every one. And gzip is still losing at 128 bytes — the last codec to stop is gzip at 160. Whatever your reasoning for compressing a 120-byte API response was, size was not part of it.

Break-even is not a win. Four of the eight rows above are sizes where the output came back exactly the same length as the input. deflate-6 at 50 bytes is the clearest case: the deflate stream itself came out 44 bytes — six saved — and the 2-byte zlib header plus the 4-byte Adler-32 spent exactly those six. Net zero.

The crossover is a first, not a last. brotli at quality 11 does not grow a 50-byte input, grows a 70-byte one, and does not grow an 80-byte one. Compression is not monotonic in the input length, so "it breaks even at 50 bytes" is a statement about the first crossing, not a promise about everything above it.

Compression overhead: output minus input, by input size

The same arithmetic on payloads people actually ship

Synthetic prefixes are honest but abstract, so the harness also compresses six bodies that look like real traffic. Every cell is outputBytes − inputBytes in bytes; negative is a win. All 54 rows round-tripped.

Payload In gzip-1 gzip-6 gzip-9 deflate-6 brotli-4 brotli-11 zstd-3 zstd-19 lz4-frame
{"ok":false,"error":"unauthorized"} 35 B +20 +20 +20 +8 +4 +2 +9 +9 +15
3f2504e0-4f89-11d3-9a0c-0305e82c3301 36 B +20 +20 +20 +8 −1 −1 +9 +6 +15
a JWT, header.payload.signature 75 B +18 +18 +18 +6 −3 −3 +9 +9 +15
{"id":1,"name":"Ada Lovelace","active":true} 44 B +20 +20 +20 +8 +4 +2 +9 +9 +15
a six-item JSON list 221 B −124 −124 −124 −136 −149 −146 −131 −136 −108
a ten-row JSON table 471 B −332 −333 −333 −345 −379 −371 −330 −358 −282

The UUID row is the one I find most interesting. A UUID string is 36 characters drawn from 16 hex digits and four hyphens — high entropy per character, but not the maximum. gzip charges its full 20 bytes and gets nothing back. brotli gives you one byte back. That is not brotli being clever; it is brotli's container being cheap enough that a handful of bits of real redundancy can actually pay for it.

The JWT row is the same story with the volume up: 75 bytes of base64url, and gzip still charges +18 while brotli returns −3. If you compress tokens individually you are paying for a container that cannot earn anything back, and if you are putting a JWT in a Set-Cookie header you have also just spent 33% on Base64 before spending anything on transport.

And the 35-byte error response — the single most common non-empty body on the modern web — is a loss under all nine codecs. There is no codec choice that fixes it. Only a threshold does.

Why gzip's overhead grows with the input

This is the part the "20 bytes" rule misses completely, and it is the reason a megabyte of data the model cannot predict comes back as a megabyte plus 328 bytes.

When deflate cannot find a match, it stops trying and copies the input verbatim. RFC 1951 calls those stored blocks, and each one costs five bytes of its own: one bit of BFINAL, two bits of BTYPE, then LEN and its one's complement NLEN. On incompressible input, gzip therefore emits the whole payload as a train of stored blocks, and the tax is five bytes per block, not twenty bytes per stream.

How many blocks you get is controlled by memLevel, which is not the knob anybody turns. Walking the block headers of 200,000 bytes of incompressible input at level 9:

memLevel Overhead Stored blocks Largest block
1 +7,893 B 1,575 130 B
2 +3,943 B 785 258 B
3 +1,978 B 392 514 B
4 +998 B 196 1,026 B
5 +508 B 98 2,050 B
6 +263 B 49 4,098 B
7 +143 B 25 8,194 B
8 (default) +83 B 13 16,389 B
9 +90 B 1 32,767 B

The arithmetic is exact, not approximate: overhead = 18 (gzip's header and trailer) + 5 × number of stored blocks. 18 + 5 × 1,575 = 7,893. 18 + 5 × 13 = 83. Every memLevel step doubles the largest stored block and roughly halves the tax, which is why a build that lowered memLevel to save memory can quietly make incompressible responses bigger.

memLevel 9 is the exception that proves the mechanism. It puts the first 32,767 bytes in a single stored block, and then the encoder changes its mind: the block walk sees one stored block and stops, because everything after it is a dynamic-Huffman block. Cost: +90 B, slightly worse than memLevel 8. The tax is not minimized by the biggest blocks — it is minimized by whichever strategy the encoder settles on, and at this input they land close together.

The level, by contrast, does nothing at all on this input:

level 0  +48   level 1  +83   level 2  +83   level 3  +83   level 4  +83
level 5  +83   level 6  +83   level 7  +83   level 8  +83   level 9  +83

Levels 1 through 9 are byte-for-byte identical on incompressible data at 200 KB. And level 0 — "do not compress" — is the cheapest of all, at +48 B: with no match search at all, zlib writes stored blocks of up to 65,531 bytes instead of memLevel-sized ones, so it pays 5 bytes six times rather than thirteen times. If your pipeline compresses video, images or anything else already entropied, level 0 is not a surrender, it is the smaller answer.

Incompressible input never shrinks

270 rows of the sweep feed the codecs bytes from a SHA-256 chain. All 270 grew. Not one codec found a single byte to remove, at any size, for any level.

Input gzip-9 deflate-6 zstd-3 lz4-frame brotli-4
1 KB +23 +11 +10 +15 +4
200 KB +83 +71 +15 +15 +5
1 MB +328 +316 +33 +15 +5

The shape of that table is the whole argument. gzip and deflate grow with the input, because their overhead is per-block and their blocks are a fixed size. zstd grows too but far more slowly, and LZ4 and brotli are flat: their overhead does not scale with length at all.

The stand-in here is a SHA-256 chain, which is the most adversarial honest choice — there is genuinely nothing to find. Real "already compressed" traffic is slightly kinder: a JPEG has structure a model can sometimes nibble at. But the ordering in that table is what you should design around. A megabyte of anything the model cannot predict comes back 328 bytes heavier under gzip and 5 bytes heavier under brotli, and both of those runs spent CPU to get there.

Which suggests the useful reflex: if the payload is already compressed, the question is not "which codec compresses best" but "which codec adds the fewest bytes", and on that question brotli and LZ4 beat gzip by two orders of magnitude.

Production already has a threshold — and it is lower than the crossover

Server operators got to this problem years ago, and the defaults they chose are visible in the docs. nginx:

gzip_min_length Default: gzip_min_length 20; Sets the minimum length of a response that will be gzipped. The length is determined only from the "Content-Length" response header field.

Cloudflare's content compression page applies its own floor: gzip needs at least 48 bytes, and Brotli and Zstandard need at least 50.

Those gates are real and they are correct as far as they go. But my measured crossover for gzip on real JSON is 160 bytes, and Cloudflare's gate is 48. Every response between 48 and 160 bytes passes the gate and comes back bigger than it went in. nginx's default of 20 is even further from the crossover. The thresholds in production are not derived from the crossover — they are derived from "is it worth the CPU at all", which is a different question with a different answer.

Worth noting what gzip_min_length reads: the Content-Length header. With no Content-Length in the response there is nothing for the directive to compare against, so a streamed body can be gzipped regardless of the configured minimum.

What to actually do with this

Rules that follow from the measurements above, and nothing else:

  1. Put a size floor on compression, and set it near 1 KB, not 20 bytes. At 45 bytes all nine codecs lost money on real JSON, and gzip was still losing at 128. Between 150 and 1,000 bytes the wins are real but small — the 221-byte list saved 124 to 149 bytes depending on codec. A 1 KB floor is where the savings stop being arguable.
  2. Never compress data that is already compressed or encrypted. Images, video, PDFs, ZIP bodies, TLS-internal anything. gzip adds 328 bytes per megabyte of that and spends CPU to do it.
  3. If the payload is small and repetitive, a threshold is the wrong fix. Both brotli and zstd have a designed answer: a shared dictionary, so the model starts warm instead of starting empty (RFC 7932 for brotli, RFC 8878 for zstd). This is the standard move for a stable API shape and it is the one thing that can make a 100-byte response worth compressing. I have not measured it here; every number in this article is dictionary-less.
  4. Watch per-message compression on sockets. HTTP responses are one body per request; a WebSocket or SSE stream with compression on every frame pays the container tax once per frame. At 40-byte frames that is nine losses out of nine, every time.
  5. Do not trust the average. Compression decisions usually get made from a mean response size, and the mean hides a long tail of 30-to-100-byte error bodies and health checks that are all losing money.

Try it in the browser

The Gzip / LZ4 compressor on this site runs gzip and LZ4 on anything you paste — text, or a file up to 20 MB, out as Base64 or hex — and prints the byte counts on both sides of the arrow, so the crossover for your payload is one paste away. Paste bytes that start with 1f 8b or 04 22 4d 18 and it reads that signature, decompresses, and moves the algorithm tab itself. It stops at those two codecs: there is no brotli or zstd button, so for those formats the interactive table at /en/benchmarks — same 553,706-byte payload, 19 codecs, all 114 rows — is what you get without leaving the browser. Format the body first with the JSON formatter — whitespace is the cheapest bytes you will ever remove, and it is removed before the codec gets a chance to charge for it.

For the specific payloads in this article: the UUID generator produces the 36-byte strings in the table, and the JWT decoder shows what the 75-byte token in that table is actually carrying before you decide to compress it.

The full benchmark this article leans on — speed as well as size, 114 rows across 19 codecs — is in Gzip vs Brotli vs LZ4 vs Zstd: Compression Benchmarks.

And the level-by-level version of this same question — every zstd level and every brotli quality on the same six payloads, 162 rows, and the point where each dial stops paying — is Zstd vs Brotli: 19 Codecs, Real Numbers.


References:

Environment: Node.js v24.22.0 on Windows x64. gzip, deflate, brotli and zstd through node:zlib (zlib-ng and brotli as bundled by Node; zstd via zlib.zstdCompressSync, available since Node v22.15.0 / v23.8.0); LZ4 frames via lz4js 0.2.0. Codecs and levels as named in each table — gzip-N, deflate-6, brotli-N, zstd-N, lz4-frame. Every run with a non-empty input was decompressed with the same library's own reader and compared byte-for-byte to its input: 783 matches, no mismatches, 18 zero-length inputs skipped. The generator is overhead.mjs and the data is compression-overhead.csv (540 rows), compression-overhead-large.csv (207 rows), compression-overhead-examples.csv (54 rows), gzip-memlevel.csv (27 rows) and gzip-level.csv (20 rows). The block-walk numbers come from reading BFINAL/BTYPE/LEN/NLEN off the deflate stream directly, in the same script. Measured 2026-11-19.

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

Related Tools

Gzip / LZ4Gunzip Base64 or hex in one step
JSON FormatterFormat, minify and validate JSON
Base64 EncoderEncode and decode Base64
UUID GeneratorGenerate UUID v4 values
JWT DecoderInspect JWT header and payload