htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Use Zopfli for gzip compression

Open lrussell887 opened this issue 1 year ago • 0 comments

I've noticed that this project is using standard gzip with level 9 compression in the scripts/dist.sh script. If Google's Zopfli compression algorithm were used instead, the minified gzip file could be made even smaller.

This can most easily be done with pigz, a drop-in multi-threaded replacement for gzip, which utilizes Zopfli when the compression level is set to 11.

I tested this against the latest 2.0.3 release of htmx.min.js:

  • gzip -9: 16,180 bytes
  • pigz -11: 15,694 bytes

This is a 3% reduction in file size. The resultant file is fully compatible with existing deflate/gzip -- decompression is unaffected.

Granted, this doesn't come entirely for free, at the expense of compute time:

root@server:~# time gzip -9 htmx.min.js

real    0m0.007s
user    0m0.003s
sys     0m0.004s
root@server:~# time pigz -11 htmx.min.js

real    0m0.146s
user    0m0.146s
sys     0m0.000s

Though I would argue the time difference is negligible and worth it, given the compress once, download many paradigm something like this project follows. Saving even just 486 bytes across the wire adds up with enough traffic.

I would definitely recommend reading this article which goes into more detail.

lrussell887 avatar Nov 05 '24 09:11 lrussell887