Compress Your Javascript

Feb 17, 2007

I think it’s pretty safe to say that javascript has become an integral part of web development. I’d also venture to say that prototype and scriptaculous are becoming must-have libraries for any javascript effort of that involves manipulating the DOM and outputting snippets of HTML.

The one downside is that once you start using javascript libraries you can quickly add on anywhere from 50k-200k worth of extra data for a page. Even though we’re in the age of broadband it can really slow down page loads. And as we know from guys like Steve Krug and Jakob Nielsen you don’t get long with your potential audience. If your page takes too long to load you might lose them.

The solution, or at least a partial one, is to compress your javascript. This essentially means removing whitespace, comments, and other non-essential characters from your javascript in order to reduce the size substantially. This means reducing your page load times significantly, especially for people with slower connections.

While there many tools out there, I’d suggest using JSMin. The source is freely available and it’s been translated into many languages, including Ruby. This means it’s probably begging to be turned into a rake task.

So while it’s a pain to have to keep two copies of each javascript file around I think the benefits are well worth the effort.