This article is a mirror article of machine translation, please click here to jump to the original article.

View: 34710|Reply: 5

[ASP.NET] asp.net core 3.1 enables the website Brotli compression algorithm

[Copy link]
Posted on 2021-1-23 12:12:25 | | | |
Brotli is a new compression algorithm developed by Google. The smaller compression response size allows for better space utilization and faster page loading. In many cases, Brotli outperforms gzip. Advantages: For typical web assets such as css, html, js, Brotli outperforms gzip by 17-25%. Brotli -11 density compared to gzip-9: html (multilingual corpus): save 25% of js (Alexa's highest 10k): save 17% of shrinking js (Alexa's top 10k): save 17% of css (Alexa's top 10k): 20% savings.

Introduction to the Brotli algorithm

Brotli was originally released in 2015 for offline compression of web fonts. Google software engineers released an enhanced version of Brotli in September 2015 with a special focus on HTTP compression. The encoder has been partially rewritten to improve the compression ratio, both the encoder and decoder have been increased for speed, and the streaming API has been improved to add more levels of compression quality. The new version also shows performance improvements across platforms and reduced memory required for decoding.

Unlike common general-purpose compression algorithms, Brotli uses a predefined 120-kilobyte dictionary. The dictionary contains over 13,000 commonly used words, phrases, and other substrings from a large corpus of text and HTML documents. Predefined algorithms can increase the compression density of smaller files.

Using Brotli instead of deflate to compress text files can usually increase the compression density by 20%, while the compression and decompression speed is roughly the same. The content encoding type for stream compression using Brotli has been proposed to use "br".

Brotli vs. Gzip


When evaluating compression algorithms, we focus on two indicators: compression rate and compression speed. As you can see in the figure above, no matter which of the 9 compression levels of gzip is used, its compression ratio is lower than that of brotli (compared to gzip, it can also be configured to 10), and the compression speed is also slower.

Brotli Advantage: Brotli outperforms gzip by 17-25% for typical web assets such as css, html, js. Brotli -11 density compared to gzip-9: html (multilingual corpus): save 25% of js (Alexa's highest 10k): save 17% of shrinking js (Alexa's top 10k): save 17% of css (Alexa's top 10k): 20% savings

asp.net core enables Brotli

Accept-Encoding header value



Code configuration

The following code demonstrates how to enable responsive compression middleware for the default MIME type and compression providers (Brotli and Gzip):


Note:

  • app. UseResponseCompression must be included in the app. UseMvc before calling.
  • Use tools like Fiddler, Firebug, or Postman to set up the Accept-Encoding request header and study the response header, size, and body.



By default, the Brotli compression provider is added to the compression provider's array along with the Gzip compression provider. When the client supports the Brotli compressed data format, the compression defaults to Brotli compression. If the client does not support Brotli, the compression defaults to Gzip when the client supports Gzip compression.

br compression test

Open the fiddler packet capture tool and use a browser to access the address of our website, you can see the following:




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

Response header: Content-Encoding: br

When compression is performed, the Content-Length header is removed because the body content changes when the response is compressed.

When compression is performed, the Content-MD5 header is removed because the body content has changed and the hash is no longer valid.


When asp.net core enables https function, Brotli will not take effect on html and json compression, but it can compress js and css. (It is possible that the character length is too small to be compressed, and it should be set

(End)





Previous:Intelligent domain name wildcard DNS lookup
Next:asp.net core bulk renames zip packets in memory
Posted on 2022-9-24 10:48:26 |
Originality is not easy, look at the source code
Posted on 2023-2-28 11:51:11 |
Thanks for sharing
 Landlord| Posted on 2023-9-1 21:59:51 |

 Landlord| Posted on 2023-9-17 10:47:02 |
.NET 6 tests Gzip and Brotli algorithms
https://www.itsvse.com/thread-10663-1-1.html
 Landlord| Posted on 2025-11-16 22:39:26 |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com