JavaScript is evolving, and together with it, its ecosystem.
Google has launched a fantastic JavaScript “compiler” – Google Closure Compiler. The name is misleading, but the tool is great. This stands behind the famous apps from Google, like GMail, Google Maps, etc. Well, let’s see this in action.
What the compiler does is to minimize and obfuscate your JavaScript code. It has three levels of optimization: whitespace only, simple and advanced. There is nothing special about the whitespace only optimization. It’s clear what it does so let’s analyze a bit the other options! In this post I will introduce the simple optimizations option, and in a later post I will write an explanation about the advanced optimizations level.
Simple Optimization Level
This is the level you should play with first. It minimizes your code and also obfuscates it. Nothing new to this point. The good news is it also optimizes it and removes unreachable code or unused code.
// say we have this code... sayHi doesn't appear to be very useful
function hello(name) {
function sayHi(){
return "hi"
}
alert(name);
}
hello('New user');
After “compilation” the code becomes:
function hello(a){alert(a)}hello("New user");
Notice the name of the global hello function remained unchanged. This is good as this is our external API, and we don’t want to break it. Closure Compiler also renames parameters, local methods and identifiers. The name parameter has been renamed to a, and notice the unused sayHi method was removed.
Let’s see other samples as well:
function hello(name) {
function sayHi(){
return "hi"
}
return "greetings";
alert(name);
}
hello('New user');
has been compiled to
function hello(){return"greetings"}hello("New user");
//with one warning (not part of the generated code):
//JSC_UNREACHABLE_CODE: unreachable code at line 8 character 2
// alert(name);
// ^
Wowww… that’s impressive. Notice the parameter to the hello method has been removed, as it is unused!
But what really got me is the “compilation” step. After all the name is not that misleading. Look at this
function hello(){
alert('hello world'
}
Well.. it’s obvious. But try and put that in your browser, and it will break your coolest app. You can avoid that with Google Closure. Here is the result:
Number of errors: 2
JSC_PARSE_ERROR: Parse error. missing ) after argument list at line 4 character 1
JSC_PARSE_ERROR: Parse error. missing } after function body at line 4 character 1
Maybe this is one of the best parts of this “compiler”.
I have tried some tests with the Google Compiler against some libraries. Here are the results:
| Library |
Library normal minified size |
Library minified size with Google Closure |
| jQuery |
55.9 KB |
54 KB |
| ExtJS Core |
78.9 KB |
74 KB |
I though the results would be significantly better with ‘closure’, but you can see the difference is not that big. ExtJS uses YUICompressor for minification and obfuscation. Maybe so does jQuery. So you can still use YUICompressor confidently. It would just be interesting to test on code which is badly written, with many unreachable statements and unoptimized.
I have done the tests with jQuery and ExtJS using the compiler.jar Google offers. Simply run the compiler from command line on the library files, which have to be previously annotated with the following comments at the beginning of the files (these comments tell Google Compiler how to make the compilation):
// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// ==/ClosureCompiler==
java -jar compiler.jar --js=ext-core-debug.js --js_output_file=ext-core.pack.js
You can also use the webapp UI Google offers at Google Closure Compiler Home.
I’ll come up with more details about the ADVANCED_OPTIMIZATIONS compilation in a later post.
Successful compilations!
>