Instructions for compressing javascript libraries

From KallestadWiki

Jump to: navigation, search

Compressing Javascript is not an intuitive task. There are several existing utilities out there that can provide javascript compression. I have found two utilities that together seem to consistently provide the best level of compression that I can find.

When compressing javascript, one thing that you should think about is renaming your variables. This is a relatively simple task when you are using unobtrusive javascript. However, if you include references to variables within your pages, or within separate javascript files, this turns into a rather daunting task. The jscompress library has two configuration settings that I find useful. One is global variable renaming, the other is local variable renaming. I haven't had any problems myself with just local variable renaming, but your mileage may vary.

The best case scenario I can imagine is when the developer utilizes unobtrusive javascript. All of the javascript files can be combined into a single text file. That text file can be run through jscompress to strip unnecessary whitespace, comments, and variable renaming. Then the file can be run through Dean Edwards's packing utility for further compression. Dean Edward's packing utility does not do any variable renaming. While this provides the best usability scenario, it also inhibits the most effective compression ratio available.

Personal tools