-
Get a monthly update on best practices for delivering successful software.
Without a doubt Ajax application are becomming more powerful. That also means that Javascript files are getting bigger and more complex. One solution is to use mod_gzip and mod_deflate (Apache) or HTTP compression (IIS). This means adding extra processing time both at the server and the client, however. One other way to cut down on bandwidth and memory is to compress the Javascript, after all, the browser doesn't need the nicely formated Javascript with white space and comments. You can just trim all of that out and save tons of space.
With that in mind, I decided to test out some of the better known Javascript compression algorithms. My test consisted of compressing three files: ClientEngine.js from the Echo2 framework, prototype.js from Prototype version 1.4.0, and a GWT ( Google Web Toolkit) compiled file from a small widget project. The GWT file was edited slightly to turn it from an HTML file into a Javascript file.
The compression tools tested were:
The results of my test are in the table below.
| File | Size | Memtronics Crunch |
Memtronics Compress |
Dojo Shrinksafe |
Creativyst | Packer | JSMin |
| Prototype | 47k | 28.91% | 69.52% | 30.96% | 18.52% | 59.40% | 23.42% |
| ClientEngine | 104k | 53.93% | 83.63% | 58.62% | 49.15% | 79.40% | 51.28% |
| GWT File | 42k | 8.11% | 52.43% | -1.00% | 0.00% | 23.30% | 5.96% |
The GWT file is already partially compressed, so a more fundamental transformation needs to happen in order to squeeze it some more. The Echo2 file was the largest and also was formatted for human consumption with comments, indentation, descriptive identifiers, etc., so it showed the greatest compression ratio.
Of the three "advanced" compressors, all of them had issues with prototype.js (syntax errors in the resulting code), but Dojo Shrinksafe didn't have any issues with the other two files. Packer seemed to work OK in IE6, but not in Firefox. Memtronic seemed to have problems in all three in both IE6 and Firefox.
The lesson? Test your compressed Javascript in all browser versions you plan to support (JsUnit, anyone?) or just use mod_deflate and company to handle your compression needs.
Related posts:
Topics: Javascript, Performance
Here’s another compressor: http://verens.com/archives/2006/04/17/jsquash-a-javascipt-compressor/
I’d be interested to see how it compares.
Comment by Kae Verens, Friday, August 18, 2006 @ 3:06 am
just tried compressing prototype with my own compressor – it takes ten minutes just to replace “element” with “Z”. on the plus side, it doesn’t hang up your browser while it’s working.
Comment by Kae Verens, Friday, August 18, 2006 @ 3:25 am
I use Memtronic’s on a regular basis, and it works great for me… however, I also use http://www.jslint.com to make sure that there are no errors in the code to begin with. Because these compressors often put everything on one line, it makes JavaScript statements that do not end with a semi-colon invalid. I used prototype about a six months ago with an earlier version of Memtronic’s compressor – it took me a week to get it right, but I was able to compress it.
Comment by Jeremy Nicoll, Saturday, August 26, 2006 @ 10:26 am
There are serious errors in these compressors, many of them won’t let prorotype-1.4.0 pass without errors.
Comment by Michal Till, Thursday, October 19, 2006 @ 8:41 pm
This one let prototype-1.4.0 pass without errors and it beats all the above compressors in efficiency too: http://www.bananascript.com/
Comment by Michael, Saturday, January 6, 2007 @ 4:12 am
Ripper Not Allowed Here…!
Just Go To Hell…!
HaHaHa…!
Comment by danielscreamo, Friday, April 18, 2008 @ 9:45 am
Just had to use some pre-made JS code, a 615Kb file for my website. Creativyst Javascript Compressor Was the only one that worked for me, not a very good compression, but, there were no problems with the “;” errors. 278 kb now, If it just had a variable shrinker option…
Comment by Wolfgang Amadeus Laser Cubria Amador, Tuesday, October 28, 2008 @ 6:32 am
[...] You can read about CSS Compressors here, and about JavaScript compressors here. [...]
Pingback by 9 Ways to a Faster WordPress Blog « WP TOY, Wednesday, March 25, 2009 @ 9:08 am
For all of you complaining about compressors failing maybe you should focus on writing clean code first. The phrase “There are serious errors in these compressors” just sounds weak. I would suggest JSLint like Jeremy said, or even Opera’s error console.
Comment by Jimmy, Friday, July 17, 2009 @ 5:48 pm