yuicompressorFollowing the release of YSlow, there has a lot buzz around the rules it evaluates. And even if not all the 13 rules are issues for normal websites, some of them, like gzipping all the static text files (CSS and JS) and minifying JavaScript are good advices also for websites with not so many users as Yahoo!.

At Yahoo! they suggested to use JSMin, a tool developed by Douglas Crockford, a guy working for Yahoo!. JSMin is only a minifier: it removes comments, unnecessary whitespace and linefeeds but does not change the name of variables or functions to shorter identifiers since it can introduce bugs not easy to debug since the resulting code is almost impossible to read.

Douglas Crockford wrote a good article explaining his decision not to obfuscate the JavaScript:

After minifying or obfuscating, you should GZIP. GZIP can further reduce the size of the program. GZIP is so effective that the difference in the efficiency between minification and obfuscation becomes insignificant. So I prefer minification with GZIP because I don’t have time for programming tools that can inject bugs into good programs.

Last week another Yahoo! engineer, Julien Lecomte, released the YUI JavaScript compressor (and yesterday he released the first update, version 1.1), which adopts a new way of minifying or obfuscating the code:

The YUI Compressor is written in Java and relies on Rhino to tokenize the source JavaScript file. It starts by analyzing the source JavaScript file to understand how it is structured. It then prints out the token stream, replacing all local symbols by a 1 (or 2, or 3) letter symbol wherever such a substitution is appropriate.

So is more than a minifier, but is safer than an obfuscator because it doesn't rely only on string replacements to change the names of local identifiers, but builds a tree of the code before replacing the names.

Tests on the YUI library have shown savings of about 18% compared to JSMin and 10% compared to the Dojo compressor (these respectively become 10% and 5% after HTTP compression)

We have been thinking about adding a minification step in Subtext build process, and now I think the time has come to do it. A test on the core js library of Subtext "common.js" shown a 13% of savings compared to JSMin (full code was 5,11Kb, JSMin 2,34Kb and YUI Compressor 1,66Kb which is almost a 70% reduction).