Modern javascript libraries tend to be fat. This increases the page load latency, wastes bandwidth and is a real problem for people with slower connections (There are still about 50% dialup users). So trying to slim things down comes almost naturally.
In this article I will try to present two compression methods that together will make your scripts lose up to 80% of their weight and I will also present an alternative way to serve gzip compressed files with Turbogears : a precompressing gzip controller for Turbogears.
The first thing one can do is javascript compression. Stripping unnecessary whitespace and renaming local variables can already bring down the size a bit, The dojo javascript compressor ( online version ) does both.
Let’s take two of the javascript libraries used for this site as examples:
This sure is nice for a start, but we can go even one step further with gzip compression. Gzip compression is a w3 standard ( defined in RFC 2616 ) and can be used to send the output of a server compressed to a client. Using this our files shrink even more:
- ff-src.js
Uncompressed: 10.1 KB
Js and gzip compressed : 2.8 KB
Compression: 73%
- tinymce.js
Uncompressed: 329.0 KB
Js and gzip compressed : 57.8 KB
Compression: 82%
Most modern web servers allow you to enable general gzip compression. IIS does it, Apache has mod_gzip for it, Turbogears can do gzip compression via cherrypy’s gzip filter.
Personally this felt like an overkill to me. I don’t really want to use my precious server CPU cycles to compress output where it’s no larger win to do so. So I wrote myself a precompressing gzip controller for Turbogears. It creates gzipped copies of a configurable set of files and automatically updates them. If the client browser supports gzip encoding, the gzipped file copy is sent, otherwise the normal file is send.
The controller is meant to be used as a subcontroller list this
class Root( controllers.RootController ):
# subcontroller
gz = GzipController()
This would map the gzip controller to /gz/ .
Download:
- gz.py — precompressing gzip controller
- serveFile.patch — Update: the patch has been integrated into the turbogears distribution