Javascript Compression

Jan
09

I have been seeing a lot of AJAX-enabled Drupal sites that are not using JavaScript compression lately, and have been wondering how many people actually know how to use it. JavaScript compression has been around for a while, but the concept is still new for some people. The technology allows you to compress the size of JavaScript files in various ways to reduce download time, bandwidth and execution time on client browsers. While it is not always necessary if bandwidth or file size is not a big concern, large scripts can take much longer to execute unless they are compressed.

Here's an example of what I mean. We recently deployed a site with a media browser component built with jQuery. Uncompressed, the file size was about 80k, compressed, the file size was about 14k. Uncompressed, there was this massive delay in Firefox and Internet Explorer while the browser figured out how to deal with the script; about 10 seconds on some slower machines. Compressed, it executed immediately and there was no interruption after the page downloaded.

The key to working with any compression program is to know how to write good JavaScript. JSLint is a debugger that verifies the quality of your JavaScript code. It looks for major errors that cause cross-browser compatibility issues as well as syntax errors that JavaScript clients have to automatically correct before they can parse your scripts. You can find JSLint at http://www.jslint.com/.

For first timers, debugging your scripts can be a little challenging. JSLint places line numbers above actual errors it finds, which can be confusing when you are looking to find where an error is happening. It is also obsessive about syntax, and will throw errors on anything it does not recognize. For instance, I like to check for values which are exactly equal to 0, but not necessarily numeric, with a double equals sign. JSLint will throw an error each time it sees this. Adapting your programming style to be compliant with actual JavaScript spec, or learning to ignore errors which are not really errors, is a big part of learning how to work with JSLint.

Actually compressing a script is a pretty simple task. In general, all you have to do is go to the web site for one of the many compressors out there, paste a script, and get back compressed code. Others, like YUI, are downloadable applications you can use to pack your scripts. YUI and Packer are my favorite compression tools - they generally provide the smallest file sizes. Compressor Rater is a site that compares the compression ratio of individual scripts with the multiple compression tools out there to see which one gives you the smallest file size. You can combine different scripts compressed with different compressors in a single web page without issue.

A word about compatibility: it is very important to check your scripts after running them through a compressor. In general, if you can get your code to verify through JSLint, you can compress it with JSMin, Packer, YUI or any of the other ones out there and expect compatibility with all browsers. This doesn't mean this will always be the case. Also, Dean Edwards, the author of Packer, has this to say about the benefits of compression over gzip. Base62 compression does add 100-200 milliseconds of overhead to parsing of JavaScript files. It also results in smaller file sizes. You do the math about which is faster.

3 Comments

A side note: while Packer

A side note: while Packer results in smaller files and the overhead is "only" 100-200 ms, this overhead applies every time that the page is loaded. Even when the file is cached. All other techniques, which rely solely on removing whitespace (not on actually compressing the code), don't suffer from this. And gzipped files are of course cached in their uncompressed form.

So, in my opinion, Packer is always a bad choice!

Packer can create issues on

Packer can create issues on older machines. jQuery can take about 100ms to unpack on each page request for an up to date machine. On a 4 year old windows box running IE 6 it can take a bit longer.

This delay can lead to JavaScript effects you have happening when the page is rendered happening enough later you could see a flash or the page jump.

If you want to use a YUI compressed jQuery instead of what core offers check out the jQuery Update module. It lets you choose the compression rate.

I typically use YUI compressed JavaScript unless it's really small.

I put some time into the

I put some time into the JavaScript Aggregator module and it now uses JSMin to compress the JavaScript.... Would be wicked to get some of these better compression methods in there.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options