Thoughts

27 Nov: Websites and Compression

One of the limiting factors in performance for web assets is the size of the content. Content authors can take steps to minimise asset size (e.g. pngcrush images). You could even automatically remove unnessecary characters from text assets like Cloudflare's Auto Minify. The next stage is to apply compression to the content so it's packed up by the server, the compressed content transfered, and then its unpacked by the client. This clearly involves extra work being done by the server and client to compress/uncompress, but the transfer size could be signficantly smaller. https://quixdb.github.io/squash-benchmark/ provides a great way to compare the various algorithms against different types of test data to see how they compare, and this show both the size benefit, and the cost in compress/uncompress.

The way negotiation works, is the client request will include an Accept-Encoding header, e.g.:

Accept-Encoding: gzip, deflate
The possible compression values can be found in the IANA registry (some clients/server also support things not listed here, like bzip2). If the server supports one of these, it can choose one, and return the compressed data, along with a header indicating the compression type:
Content-Encoding: gzip
Using Nginx as an example, in order to gzip content you might do this:
gzip            on;
gzip_min_length 2000;
gzip_types      text/html text/plain;
gzip_comp_level 7;

Which says enable gzip compression when the response is going to be 2000 bytes or more and the MIME type is text/html or text/plain and compress with gzip level 7.

Relatively new on the scene is a compression algorithm developed by Google in 2015 called Brotli. There's a few comparisons around showing for many use cases, Brotli provides significantly better compression whilst also being quicker. Nginx can be built with a module to support Brotli compression. Browser-wise, Brotli is widely supported:

Ref: https://caniuse.com/#feat=brotli. So the takeaway, is if you're serving big enough files to make a difference, it may be worth enabling compression, and if you do, likely Brotli will give the best results.
© 2017