5x12 pentomino tiling
«

Minifying

»

Today's Word of the Day is "minify" which is, to my surprise, a real word - in American English at least - not one made up by programmers. In general terms it means to make small or smaller but in computer terms it has a more specific meaning: to reduce the size of source to make it load, parse and execute faster.

For those of you programming in compiled languages doing minifying at all is a strange concept but I routinely program in PHP and JavaScript, neither of which are compiled in advance and also write cascading style sheets for web pages. The last two are obvious candidates for minifying: it reduces the amount of data to be sent to the client browser, it gives the browser's parsing engine less bytes to chew on and, in the case of JavaScript, it allegedly makes the script run faster1.

Google claim it's worth minifying any JavaScript script of 4K or larger in size and claim that you should see a benefit for any file that can be reduced by 25 bytes or more.

I had been dimly aware of all of this for some time, mainly because I'd come across JavaScript that had been minified. It looks like line noise: the entire script typically on one line with all the local variable names reduced to one or two characters. And that is pretty much the trick to minifying JavaScript: you strip out the whitespace and comments and make the variable names as short as possible.

My recent project with maps meant the user's browser had a 25KB JavaScript script to download and run. Would minifying help? So I looked around for a tool2. There's several out there. I went with YUIcompressor which is written in Java and seems to do the job.

So now when I publish a script onto an external web site if the JavaScript includes the string ":YUI:" in the comments the source is minified automagically and the minified version is uploaded to the web site. To keep our rights intact a minimal copyright message is added after minifying. If you are interested you can see the results here but the bottom line is that the script is over 25% smaller after minifying. Not bad. Even if it doesn't parse and run faster (and I suspect it does although I've not clocked it) it will certainly download a little faster and help reduce bandwidth costs.

So next stop is do the same for my CSS.

And once I'm happy that this is reliable I'll drop the ":YUI:" and do it for everything.

Anyway, based on my brief experience to date I can recommend minifying (even if it is an ugly little word).

  1. By the way for PHP the usual solution is to use a caching accelerator - so the first time a script is invoked it's compiled and the compiled version is then cached by the server. We've used a variety of them on our servers over the years and they do help speed things up.
  2. As you'll gather yesterday's migraine did go in the end and by 4pm I was, as is pretty typical, champing at the bit to get something done!

Tags: JavaScript, words Written 13/07/10

Comment on this article

«
You can follow these posts on Twitter at @Wibblings
»
I am currently reading:

Faster, Higher, Farther by Jack Ewing The World of Mr Mulliner by PG Wodehouse

(?)
Word of the Day:
fakelore