Google Closure Compiler Service
The Google Closure Compiler (online service here) is a great tool to optimize (shrink) javavascript code, reducing the files size for faster loading. It has 3 optimization levels, to suit your needs (whitespaces only, simple and advanced. The first 2 optimization levels are kinda straight forward, but the advanced mode (best optimizations) needs some adjustments in your code.
it detects unused code, so if you are trying to optimize a library that only has functions that are used somewhere else, you will end up with an empty result (nothing is used). The solution for this can be found here:
function displayNoteTitle(note) { alert(note['myTitle']); } // Store the function in a global property referenced by a string: window['displayNoteTitle'] = displayNoteTitle;
it is very picky about undeclared functions. If you are trying to optimize a library that uses jQuery, it will rename calls to methods it does not know, so $('#abc').text('text');becomes something like $('#abc').a('text');. The solution for that is to declare an externs url for the jQuery library. On the online service, that can be done by using the following header
// ==ClosureCompiler== // @compilation_level ADVANCED_OPTIMIZATIONS // @output_file_name default.js // @externs_url https://closure-compiler.googlecode.com/git/contrib/externs/jquery-1.9.js // ==/ClosureCompiler== // ADD YOUR CODE HERE
Other useful pieces of information about Closure Compiler:
link Closure Compiler assumes that you are using gzip compression, and it may make your uncompressed file bigger than it was