I just like the word “minification”.
Anyway, here’s the deal. in ActionScript, and most other languages that go through some sort of compilation process, you write your code in source files and that human readable text gets distilled down into something else. In AS3, it’s byte code that runs in the Flash Player. In Java, it’s byte code that runs in the Java Virtual Machine. In .net languages it’s also similar, as things run on the CLR. Other forms of C or C++ will compile down into something closer to what the computer itself can understand.
With JavaScript, however, you put your files up on your website and the browser pulls them down and executes them. This means that every line of code, every character you write, comment, whitespace, long variable name, etc. has to be uploaded in full, and downloaded in full by every person who visits your site. This further explains a lot of the terse structure of much of the JavaScript you see.
This, of course, presents a problem. Good code, maintainable, readable code, needs to have some structure and some comments. If you’re writing an API, you need to comment it, ideally in the code, and have something that generates documents from those comments. Other, non-obvious points need to be documented as well. Whitespace is important for readability, as are well named functions and variables. But all of this makes bigger files.
Furthermore, while you might want (should want) your code to be understandable and maintainable by your team, you probably don’t care so much whether the end users of your site are able to read the code, and in fact in many cases, may prefer that it is UNreadable by them.
Computer people being smart guys and gals, someone came up with a solution called minification. The idea is to take your source files and run them through a program that makes them as small as possible, while still retaining the same functionality. You continue to code and upgrade and maintain the original source files, but only the minified versions go out on the web. It’s essentially the same workflow as compiling a release version of an app. You keep the original, uncompiled source, you send out the compiled product, once you are ready to ship.
So what does a minifier do? Well, obviously, it can remove any comments and unnecessary whitespace. It can also replace variable and function names with smaller names. If that’s all they did, you’d see some moderate decreases in size and readability. But some go way beyond that, creating files that you would bet money would not execute, and bear no resemblance to what you wrote.
There are probably multiple tools out there that do this, but here’s one for example: http://jscompress.com/
All you do is paste in your code, hit the button, and save the results. Let’s try it with the source of wirelib.js. I guess you are pretty familiar with what that looks like. At this writing, it’s 217 lines of code. Plenty readable. If I paste it in and hit the compress button with the default “Minify (JSMin)” option, I get this:
[php lang=”JavaScript”]var wirelib=(function(){var canvas,context,width,height,lines=[],cx,cy,cz,fl=250,interval,running;function initWithCanvas(aCanvas){canvas=aCanvas;if(canvas!==undefined){width=canvas.width;height=canvas.height;cx=width/2;cy=height/2;cz=fl*2;context=canvas.getContext(“2d”);}}
function project(p3d){var p2d={},scale=fl/(fl+p3d.z+cz);p2d.x=cx+p3d.x*scale;p2d.y=cy+p3d.y*scale;return p2d;}
function addLine(){var i,numPoints,points,line;points=(typeof arguments[0]===”object”)?arguments[0]:arguments;numPoints=points.length;if(numPoints>=6){line={style:this.strokeStyle,width:this.lineWidth,points:[]};lines.push(line);for(i=0;i
Please don’t use the “Packer”. I minified it with google closure compiler and then microsoft ajaxmin. The result is 2759 bytes uncompressed.
Tools used:
http://closure-compiler.appspot.com/home
http://ajaxmin.codeplex.com
You can find the minified file here:
http://dl.dropbox.com/u/2199036/wirelib.min.js
Nice post Keith; Minification is pretty interesting.
Is there some server side script that could auto-minify your JS when requests are made – so you can keep your original spaced, commented JS files on your server (although inaccessible to the users) – so you don’t have to minify before every update? Or would that be a bad idea for other reasons?
Would be interesting if there are any differences regarding the peformance ..
http://code.google.com/closure/ Google closure is also a great tool you can also use it to obfuscate your JavaScript as well. I highly recommend looking into this tool. Mr. Peters thank you for the excellent JavaScript tutorials! I really enjoy reading your blog and always learn something new. Thanks
If you are using Haxe for Javascript or another Javascript project within FlashDevelop, you should download the YUI Compressor (http://yuilibrary.com/downloads/#yuicompressor). Then you could build the minified version automatically.
Grab the ‘yuicompressor.jar’ file from the ‘/build’ folder inside the zip. Put this .jar-file in the root of your project inside FD.
In FD go to project>properties>build and add this line in the ‘post-build command line’:
java -jar yuicompressor.jar bin/$(ProjectName).js -o bin/$(ProjectName).$(BuildConfig).min.js
BTW: A great tool for ‘unminification’ is http://jsbeautifier.org/ This also could unpack packers.
Thanks for all the other links. As I hope I conveyed, this is just stuff I’m learning about myself, so in no way am I saying this one is good or this one is bad. Just giving some examples of what’s out there.