Skip to content

Brotli Compression

Abstract

Compress assets such as JavaScript and CSS using Brotli rather than Gzip.

Brotli is a successor to Gzip and you can roughly expect a 15% reduction in response size when using Brotli vs. Gzip. If your website still uses Gzip, prioritize turning on Brotli everywhere. Such a small change (likely just a toggle on your CDN's dashboard) and can have a significant impact to users who aren't on a good Internet connection.

Let's take an example and do a napkin calculation to assess just how much better Brotli is compared to Gzip.

brew install brotli gzip

# Generates random data for testing.
jot -r -c 100000 > random.txt

# Compresses using Gzip.
gzip -c -k -1 random.txt > gzip-q01.gz
gzip -c -k -2 random.txt > gzip-q02.gz
gzip -c -k -3 random.txt > gzip-q03.gz
gzip -c -k -4 random.txt > gzip-q04.gz
gzip -c -k -5 random.txt > gzip-q05.gz
gzip -c -k -6 random.txt > gzip-q06.gz
gzip -c -k -7 random.txt > gzip-q07.gz
gzip -c -k -8 random.txt > gzip-q08.gz
gzip -c -k -9 random.txt > gzip-q09.gz

# Compresses using Brotli.
brotli -k -q  0 -o brotli-q00.br random.txt
brotli -k -q  1 -o brotli-q01.br random.txt
brotli -k -q  2 -o brotli-q02.br random.txt
brotli -k -q  3 -o brotli-q03.br random.txt
brotli -k -q  4 -o brotli-q04.br random.txt
brotli -k -q  5 -o brotli-q05.br random.txt
brotli -k -q  6 -o brotli-q06.br random.txt
brotli -k -q  7 -o brotli-q07.br random.txt
brotli -k -q  8 -o brotli-q08.br random.txt
brotli -k -q  9 -o brotli-q09.br random.txt
brotli -k -q 10 -o brotli-q10.br random.txt
brotli -k -q 11 -o brotli-q11.br random.txt

# Lists sizes of all files.
ls -lnh .

195K random.txt
108K gzip-q01.gz
107K gzip-q02.gz
106K gzip-q03.gz
104K gzip-q04.gz
105K gzip-q05.gz
103K gzip-q06.gz
103K gzip-q07.gz
103K gzip-q08.gz
103K gzip-q09.gz
127K brotli-q00.br
106K brotli-q01.br
 98K brotli-q02.br
 99K brotli-q03.br
 99K brotli-q04.br
 98K brotli-q05.br
 98K brotli-q06.br
 98K brotli-q07.br
 98K brotli-q08.br
 98K brotli-q09.br
 87K brotli-q10.br
 87K brotli-q11.br

The uncompressed file is 195kb. The best Gzip compression outputs a 103kb file. The best Brotli compression outputs a 87kb file. That's a (103 - 87) / 103 * 100 = 15% optimization!

The browser sends the br hint in the Accept-Encoding HTTP header if it can decode Brotli resources.

// Request
Accept-Encoding: gzip, deflate, br

// Response
Content-Encoding: br