A short history
Brotli was created by Jyrki Alakuijala and Zoltán Szabadka at Google in Zurich. The first version from 2013 was made for one job only: compressing web fonts in the WOFF2 format. In 2015 Google released a general purpose version, and in July 2016 it became an internet standard as RFC 7932 (opens in a new tab). Chrome, Firefox and Edge added support in 2016 and 2017, Safari in version 11 (2017). According to Can I use (opens in a new tab), about 97 percent of global web traffic comes from browsers that accept it today. The name is Swiss German for a small bread roll.How Brotli works
Brotli builds on the same two steps as gzip: find repeated byte sequences and replace them with back references, then encode everything with short bit codes. On top of that it adds three things.A much bigger window
Gzip can only look back 32 KB for a repeat. Brotli can look back up to 16 MB. A Unity WebGL build is one big WebAssembly file plus one big data file, and in files like that the same byte patterns show up megabytes apart. When gzip has long forgotten them, Brotli still remembers.A built-in dictionary
Brotli ships with a static dictionary of 13,504 common words and phrases, 122,784 bytes in total, baked into every encoder and decoder. It contains things likefunction, <div class=", Content-Type and many English words. When the compressor sees one of these in your data, it can reference the dictionary even if the phrase never appeared in the file before.
On top of the 13,504 base words there are 121 transforms, for example "make the first letter upper case" or "add a space after it", so one entry can stand in for many spellings.
The dictionary helps a lot for HTML, CSS and JavaScript. For a WebAssembly binary or a texture it helps very little, but the bigger window and the next feature still apply.
Context modeling
Gzip uses one Huffman table per block for all literal bytes. Brotli can switch between several tables depending on the previous one or two bytes. After a< in HTML, the next byte is probably a letter. After a byte of a float in a mesh, the next byte follows a different pattern. Picking the table based on context lets Brotli spend fewer bits on each byte, and this is where a lot of the extra savings on binary data come from.
Quality levels 0 to 11
Like gzip, Brotli has levels, but twelve of them. Levels 0 to 4 are quick and meant for compressing on the fly. Levels 5 to 9 search harder. Levels 10 and 11 do a very expensive extra pass and are meant for files you compress once and serve many times. Unity uses the top level for WebGL builds, which is why Brotli builds take noticeably longer than gzip builds.The numbers
Here are results from Google's own 2015 study (opens in a new tab) on the Canterbury corpus, a standard test set of 11 text and binary files. The ratio is original size divided by compressed size, so higher is better.| Algorithm | Compression ratio | Compression speed | Decompression speed |
|---|---|---|---|
| deflate (gzip) level 1 | 2.91 | 93.5 MB/s | 323 MB/s |
| deflate (gzip) level 9 | 3.37 | 15.5 MB/s | 347 MB/s |
| Brotli level 1 | 3.38 | 98.3 MB/s | 334 MB/s |
| Brotli level 9 | 3.97 | 17.0 MB/s | 355 MB/s |
| Brotli level 11 | 4.35 | 0.5 MB/s | 290 MB/s |
| Algorithm | Compression speed | Decompression speed | Size vs. original |
|---|---|---|---|
| zlib (gzip) level 6 | 26.8 MB/s | 314 MB/s | 32.2% |
| zlib (gzip) level 9 | 11.3 MB/s | 318 MB/s | 31.9% |
| Brotli level 5 | 53.3 MB/s | 419 MB/s | 28.1% |
| Brotli level 8 | 12.6 MB/s | 443 MB/s | 27.0% |
| Brotli level 11 | 0.56 MB/s | 383 MB/s | 23.8% |
A real world case
LinkedIn measured (opens in a new tab) what this means for a big JavaScript file from their site. Brotli level 11 made the file 30 percent smaller than gzip level 6. The compression time went from 169 ms to 11,913 ms, while decompression time dropped from 35 ms to 22 ms. After switching, their page load time at the 90th percentile improved by 2 to 3.6 percent in the US and 6 to 6.5 percent in India, where connections are slower. For a Unity WebGL build, a developer in a Unity forum thread (opens in a new tab) posted an empty scene build in Unity 2021.2 at 2.9 MB with gzip and 2.2 MB with Brotli, about 24 percent smaller. The Brotli compression step in the build log took 51 seconds.The catch
Browsers only accept Brotli over HTTPS. Over plain HTTP they do not even announce support. For a game on a web portal or your own domain with a certificate this is not an issue. For a local test server without HTTPS it is. The second catch is that your web server has to send the headerContent-Encoding: br for the .br files Unity produces. If it does not, Unity's loader either fails or falls back to a slow JavaScript decompressor. Unity has sample configurations (opens in a new tab) for Apache, Nginx and IIS.


