MiniME JavaScript Minifier
Sunday, 15 August 2010
While suffering a severe case of "not invented here" syndrome I decided it would be a good idea to write my own JavaScript minifier... as you do.
Recently I've been doing a lot of client side JavaScript web programming and it became apparent that I needed a good minifier. In the past I've used YuiCompressor and Google's Closure Compiler, both of which worked well - but I think I just wanted something a little different.
Or perhaps I didn't want to introduce a dependence on the Java Runtime in my build environment, perhaps I was just interested in the challenge of writing my own, or perhaps it was a case of not invented here syndrome. Either way, the result is MiniME!
MiniME is a JavaScript minifier written entirely in C#. It can be invoked through the command line, or programatically from .NET code and runs under Windows or Mono 2.6 and later.
MiniME supports all the things you'd expect from a JavaScript minifier - removal of comments and whitespace, obfuscation of local variables, parameters and functions. It can automatically detect and remove constants (great for enums).
By default MiniME does nothing that might cause your script to no longer work, so member variable and methods are not obfuscated. With a few simple comment directives however you can select members that are safe for obfuscation and thereby further reduce the size of your code.
It supports opt-in comment preservation (for copyright notices), combining of multiple scripts into one library, command line response files, file change detection and a few other miscellaneous features.
So what sort of size reduction will MiniME yield? In short it's better than YuiCompressor, but not quite as good as Closure. Here's a quick comparison:
| File | Original Size | Yui | Closure | MiniME |
|---|---|---|---|---|
| jQuery-1.4.2.js | 170,095 | 79,702 | 71,850 | 73,763 |
| jQuery-ui-1.8.2.js | 352,216 | 207,928 | 192,217 | 195,909 |
| "mylib" * | 108,079 | 46,833 | 42,977 | 33,367 |
* Closure was run in simple optimization mode.
** mylib is a placeholder name for a library I'm working on. It has a few directives to enable obfuscation of internal members - hence the smaller size.
So as you can see, MiniME doesn't do too bad a job for a quick little project written over a few weeks.
More information, download and source code for MiniME can be found here: http://www.toptensoftware.com/minime.
1 Comment
Leave a comment
Awesome! I've been looking for a no-nonsense JS minifier. Just tried it on our project here, better compression than Yui, not as complicated as Closure and no Java dependency! Nice work!