The Difference between Brotli vs gzip to speed up your site

The difference between Brotli vs gzip to speed up your site

HTTP compression is a built-in capability in most modern web servers and clients. This reduces the size of data or the amount of data transfers. In turn, such compression requires much less bandwidth and increases the website’s performance. These compression algorithms offer up to 80% compression for some documents. Brotli vs gzip are the two most popular compression algorithms used today. The gzip compression algorithm comes by default on most web servers, making it easier to utilize. However, Brotli may offer better performance depending on the use case. Let’s compare these compression algorithms and learn the differences between them.

Detect compression algorithm

You can check which compression algorithm is used on your browser by opening the developer tools. Cick on the files under the “Network section”. If the website uses Cloudflare or other CDN, it might not display the compression algorithm.

Another method that can be used is the curl command.

curl -s -I -H "Accept-Encoding: gzip, deflate, br" https://example.com

Of course, you must replace the example.com domain with the website your are testing.

Gzip

Gzip is based on the deflate algorithm. It uses both LZ77 and Huffman coding lossless data compression algorithms to compress data.
The gzip compression algorithm offers compression rates of 60-70% reductions of web content. This includes HTML, CSS and Javascript and it also offers fast decompression. It is widely supported by major web browsers and is the default method on web servers.

To compress data gzip identifies repeated sequences and stores a reference to the first occurrence. It replaces subsequent occurrences with pointers to that original instance, instead of storing the same data multiple times. It uses LZ77 for this compression. Then using Huffman coding it represents the frequently occurring characters. It combined shorter binary codes and less frequent characters with longer codes to further compress the data.

Brotli

Brotli uses a variant of the LZ77 algorithm, combined with Huffman coding and dictionary-based compression. Its dictionary includes common strings and phrases found in web content. This makes it particularly efficient for compressing small files like HTML and CSS. Brotli has better compression rates compared to gzip offering higher compression.

Brotli is also supported by major web browsers and CDNs like Cloudflare.

Compare gzip and Brotli

Let’s compare gzip and Brotli and find the differences in both compression algorithms.

High Compression Ratio: Brotli often achieves 15-25% better compression rates than gzip. This makes it ideal for web assets where size reduction is crucial for faster load times.

Decompression Speed: Brotli might be slightly slower than gzip but still fast and supported by modern browsers.

Brotli provides better compression but takes more CPU time and resources to compress files. For static content (files that don’t change often), Brotli’s slower compression speed might not be an issue. These the files can be compressed once and served repeatedly from the cache. For dynamic content, Brotli’s slower compression may introduce performance overhead unless proper caching is implemented.

Compression Levels
Both Brotli and gzip offer multiple levels of compression:
• Gzip: 1 (fastest, least compression) to 9 (slowest, best compression).
• Brotli: 0 (no compression) to 11 (best compression).

At maximum compression levels (Brotli 11 and gzip 9), Brotli can significantly outperform gzip in terms of file size reduction. However, this may take much longer to compress. However, at lower levels (Brotli 1-4 and gzip 1-4), Brotli is still often better than gzip while maintaining faster speeds.

When to use Brotli vs gzip

This will vary on the type of content you are serving and the content of those web files. For best performances you may need to test to ensure you choose the best compression algorithm for your use case.

When to use gzip

  • Dynamic Content: For content that changes frequently gzip might be a better choice because of the faster compression speed. This can include user-generated pages or APIs.
  • Compatibility: If you’re dealing with legacy systems or browsers, gzip is still more widely supported than Brotli.
  • On-the-fly Compression: Since gzip compresses data quickly, it’s useful when you need real-time compression for every request.

When to Use Brotli:

  • Static Assets: Brotli is ideal for static content like CSS, JavaScript, and HTML files. They can be compressed once and cached for reuse. The slower compression speed doesn’t matter since these files aren’t recompressed frequently.
  • Higher Compression Ratio: If you’re optimizing for slow connections or mobile users, Brotli’s higher compression ratio can be very beneficial.
  • Modern Browsers: Brotli is supported by all major modern browsers. It is also a safe choice for websites targeting an up-to-date audience.

Conclusion

Brotli offers higher compression rates than gzip often making it a better choice, but for dynamic content and on-the-fly compression Gzip is still faster. You may still need to test out which compression ration to use to best optimize your website performance. Of course, you would not have to worry about this if you are using our Linux VPS hosting services, you can simply ask our expert Linux admins to optimize your website performance for you. They are available 24×7 and will take care of your request immediately.

If you liked this post on the differences between Brotli vs Gzip to speed up your site, please share it with your friends on social networks or leave a comment in the comments section.

Leave a Comment