JSON Formatter โ€บ Guides

Is minifying JSON still worth it once gzip or Brotli is on?

Open the tool: JSON Formatter

Minifying JSON means removing the spaces and line breaks that make it readable. On disk the saving is large, and the advice to minify everything follows from that. But JSON sent by a web server is often compressed on the way, and compression is very good at exactly the thing minifying removes: the same run of spaces repeated on every line. We measured five documents in four layouts, before and after compression, to see how much of the saving is left.

Short answer: without compression, JSON indented with 2 spaces was 26 to 140% bigger than the minified version. After gzip the gap fell to 3 to 21%, and after Brotli to 2 to 8%. Gzip itself made the minified files 61 to 88% smaller. Turn compression on first, then minify what a program reads, and keep indentation where people read the file.

What we measured

Five documents were written out in four layouts with JSON.stringify: minified, 2 spaces, 4 spaces and tab. Four were generated for the test: an API response with 2,000 user records, a map line with 20,000 longitude and latitude pairs, a settings file nested 6 levels deep, and a log of 5,000 short events. The fifth is a real file, the tool list this site serves at /tools.json, as it was on the day of the test. Each of the 20 files was then compressed with gzip at level 6, the zlib default, gzip at level 9, and Brotli at quality 11, using zlib 1.3.1 and Brotli 1.1.0 in Node.js 22.23.4. Sizes are in KB of 1,000 bytes.

Before compression: indentation is expensive

DocumentMinified2 spaces4 spacesTab
API response, 2,000 users426.3 KB640.3 KB808.3 KB556.3 KB
Map line, 20,000 points455.7 KB1,095.7 KB1,655.8 KB815.7 KB
Config, 6 nesting levels124.0 KB239.6 KB338.8 KB190.0 KB
Log, 5,000 events284.7 KB434.7 KB534.7 KB384.7 KB
Tool list (tools.json)17.4 KB21.9 KB25.1 KB20.2 KB

The map line more than doubled with 2 spaces because every coordinate pair became four lines, each with its own indentation. The tool list grew least: 77% of its minified bytes are quoted text such as names and descriptions, so whitespace is a small part of the file.

After compression: most of the gap disappears

The next table shows how much bigger the 2-space file is than the minified file, at each stage.

Not compressedgzip level 6Brotli quality 11API response50.26.22.9Map line140.514.57.9Config93.221.47.1Log52.714.13.1Tool list25.92.62.2
Extra size of 2-space JSON compared with minified (%) โ€” chart drawn from the table below.
DocumentNot compressed (%)gzip level 6 (%)Brotli quality 11 (%)
API response50.26.22.9
Map line140.514.57.9
Config93.221.47.1
Log52.714.13.1
Tool list25.92.62.2

In bytes, minifying the API response saved 214.0 KB on disk and 6.2 KB after gzip. For the map line it was 640.0 KB against 16.2 KB, and for the tool list 4.5 KB against 0.2 KB. The saving does not vanish, because the compressor still has to record where each line break and indent goes, but it shrinks to a small fraction.

The other layouts followed the same pattern. With gzip at level 6, 4 spaces cost 3.5 to 28.5% more than minified and tab cost 1.8 to 13.8%. The deeply nested settings file was the worst case in every layout.

Compression matters far more than minifying

DocumentMinified, not compressed2 spaces, gzip level 6Minified, gzip level 6Minified, Brotli 11
API response426.3 KB107.5 KB101.2 KB76.0 KB
Map line455.7 KB127.6 KB111.5 KB76.0 KB
Config124.0 KB33.2 KB27.3 KB23.7 KB
Log284.7 KB38.7 KB33.9 KB27.6 KB
Tool list17.4 KB6.9 KB6.7 KB5.7 KB

For every document, the indented file with gzip was much smaller than the minified file without it: 107.5 KB against 426.3 KB for the API response. Gzip at level 6 made the minified files 61 to 88% smaller, and Brotli at quality 11 made them 67 to 90% smaller. Moving from gzip to Brotli also saved more than minifying did after gzip in four of the five documents. If a server sends JSON without compression, fixing that is the first step, and minifying is the second.

When minifying is still worth doing

When to keep the indentation

To switch a pasted document between the two layouts, use the JSON formatter: Minify removes the whitespace and Beautify puts it back with 2 spaces, 4 spaces or a tab.

Limits of this test

Four of the five documents were generated, with random names, numbers and identifiers. Real data may repeat more, and then both compression and the remaining gap will differ. The compressors were run on whole files at fixed settings. A server that compresses responses as they are produced may use a lower Brotli quality than 11 and get results closer to the gzip columns. The test measured size only, not the time to compress, send or parse.

The measurements are in minify-compression.csv: one row per document and layout, with columns document, format, raw_bytes, gzip6_bytes, gzip9_bytes and brotli11_bytes.

Common questions

Does minifying change the data?

Removing whitespace does not. Spaces, tabs and line breaks between values have no meaning in JSON, so a parser reads the same values from both versions, and spaces inside quoted strings are left alone. A tool that parses the text and writes it out again, as our formatter does, also rewrites numbers in a standard form, so 1.0 becomes 1.

How do I know whether a response was compressed?

Open the browser's developer tools, select the request in the Network panel and look at the response headers. A value such as Content-Encoding: gzip or br means it was compressed. The panel also shows the transferred size next to the uncompressed size.

Is a tab smaller than 2 spaces?

Yes, in every document we tested, both without compression and after gzip. Without compression, tab-indented files were 16 to 79% bigger than minified, against 26 to 140% for 2 spaces.

Should I shorten the key names too?

That is a separate change from minifying, and it makes the data harder to read and maintain. Repeated key names are the kind of repetition compressors handle well, so measure the compressed size before deciding.

References

  1. RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format
  2. RFC 1952: GZIP file format specification
  3. RFC 7932: Brotli Compressed Data Format
  4. MDN: Content-Encoding header

Figures without a source link come from the steps shown on this page. Companion tool: JSON Formatter. Spotted an error? Write to help@glowhalo.org.

More guides

Text & developerWhy do word counters disagree? One text counted four ways in seven languagesWe counted the same document with four common rules. In English they were less than 0.4% apart. In French the gap reached 10%, and in Japanese one rule found 92 words and another 2,171.Read guide โ†’Text & developerDoes Base64 really make data 33% bigger? We measured it before and after compressionBase64 added about 33% to every file we tried. After gzip the cost was under 1% for images and 40 to 65% for text. What that means for data URIs, JSON and email.Read guide โ†’Text & developerUUID v4 or v7 as a database primary key? We inserted a million rows of eachRandom v4 keys were 3โ€“4.7 times slower to insert than time-ordered v7 keys in our SQLite test. What causes the gap and when it matters.Read guide โ†’