Javascript Compression With Googles Closure Compiler

Google have now released their Closure library and included in that is a really useful javascript complier.

Yes, another one. This one is different though, it is smarter. Just like JS Lint and all YUI Compressor it minifies and compacts your code; but it also does things like removing unused code!

Reductions

Obviously every piece of code will differ, but on the example code I've tried the reduction in size is staggering. I tried a single javascript file which is originally 24k, on the highest level it ended up at 2k!

Here is a really simple form that takes the javascript and runs it though the Google Closure Complier

From URL:

You will be redirected to the complied version of the code when you submit the form.

From Source:

You will be redirected to the complied version of the code when you submit the form.

If you want to see just how clever it is, try using this code below in all the various compressors..

function myalert(alert_var){
  alert(alert_var);
}
function myalert2(alert_var){
  alert(alert_var);
}
myalert("hello");

At the highest level using googles compiler it returns just alert("hello").

By Charles Marshall