Skip to content

Brotli Compression


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 random.txt
brotli -k -q  1 -o random.txt
brotli -k -q  2 -o random.txt
brotli -k -q  3 -o random.txt
brotli -k -q  4 -o random.txt
brotli -k -q  5 -o random.txt
brotli -k -q  6 -o random.txt
brotli -k -q  7 -o random.txt
brotli -k -q  8 -o random.txt
brotli -k -q  9 -o random.txt
brotli -k -q 10 -o random.txt
brotli -k -q 11 -o 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

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