Wednesday, November 18, 2009

"Compiling" Javascript Code

Look what I found this morning. It's a "compiler" for Javascript code. Since Javascript is interpreted instead of compiled it's not truly a compiler but it does something similar: makes the code readable for the machine and takes out all of the extra fluff that a carbon based life form needs for comprehension.

I have tested it with some of the javascript that I use on my personal sites and got about a 25% average compression.

I looked at the largest piece of Javascript on the home page. It is the jQuery UI javascript code at 188K. It compressed down by 11%. The other way to really compress the Javascript is to use gzip. Here is a blog post by someone else about it (he also mentions some other utilities for doing what Google closure is doing). Basically, you zip the Javascript and then, since it runs in the visitor's browser anyway their browser expands the file to run the code after they've downloaded it. Gzip is achieving near universal installation, so this method of compressing Javascript has less risk of breaking than it did a year ago.

In essence, what the Closure "compiler" is doing is removing all of the whitespace and pretty variable names that we use so that we can read the code. It's very much like the "Optimize for Web" settings for image programs which remove all of the color descriptions for unused colors and the like.

The home page for the Closure compiler is here and it also explains the philosophy behind what they are doing. It works in conjunction with Page Speed. I've started looking at these things recently because I was reviewing the analytics for our organization's web site and noticing that people are still hitting the site with dial-up. I'm also noticing an uptick in mobile visitors who might be using EDGE or other slower networks to hit our site. Living in an urban area with good G3 coverage and broadband in most places, we forget the costs associated with Flash effects and high-res widget graphics. The 2007 Census numbers show that 10% or so of Americans with home Internet access were using dial-up.

As long as any of those people want to be a part of our organization, we need to make sure they can access our information. Besides, a flashy website is not necessarily the only sign of organizational quality. Take a look at this company's website for instance. The look is dated but clean and speedy.

No comments:

Post a Comment