Linking Javascript

This is an idea that I’ve been kicking around for a while.

Back in the C days, when most everything was determined at compile time, life was simple. You wrote your program, you preprocessed it, you compiled it, then you linked it. Linking is the cool part. The linker would determine exactly which bits of code you were using, and selectively remove everything else. This dramatically decreased final executable size and improved efficiency. As a side effect, if you called a function that wasn’t defined in your code, the linker produce an error about “unresolved symbols,” which would remind you to link to your favorite library.

That was then. Now we have compiled languages with dynamic runtime typing & generic programming, runtime interpreted languages like python, and the “anything goes language,” javascript.

Various web projects that I’ve worked with use various javascript libraries. These things are cool, protoype.js, mochikit, jquery, etc., all put some serious power in the hands of the saavy web developer. But, they come with an implicit inefficiency: I have to include the entire library on each page that I use it on. And, since it’s not compiled, javascript won’t tell you about errors until you actually run the program.

Now this is not to say that the modern javascript engine isn’t screamin’ fast. There has been some amazing work done to make javascript go faster. And there are some great javascript IDEs out there like Aptana. But just like with the old languages, the best optimizing javascript compiler in the world won’t help reduce code bloat. And Aptana will help you with library code, but only with special support for popular libraries.

My premise is this: I want to be able to include an entire library, say mochikit (about 200 KB), call one or two functions in it, run a static lexical javascript “linker” remove all of the unneeded code. Now instead of 200 KB of library code, maybe I have 2 KB. If that were possible, then I’d reduce my page download size significantly, and drop the burden on the client machine equally significantly.

Those of you in the know will probably say “Just turn on server-side gzip compression” or “200 KB is only downloaded once, then the browser caches it.”
Both points are right. However, javascript is code, so by it’s nature, compression ratios will be relatively low (relative to text or html). And yes, smart browsers will cache the javascript file, and maybe even compile the js code for later. But I still have 198 KB of code consuming memory in my client’s browser that isn’t doing anything. One page with 198K of unused code might not be a big deal, but how about two or three or 20? (I have 28 tabs open in two browsers right now, but I may be a special case)

Those of you really in the know will make the more pertinent observation that javascript is highly dynamic, making it difficult or impossible to detect if a particular bit of code is used through lexical analysis alone. You would be right, there are plenty of cases where it would be. Anything touched by the exec() would be trouble, not to mention classes. There are lots of things to work out, and there would be limitations.

I have been searching to see if this has already been done (a common phenomenon with ideas that I have). It took a while†, but I did locate a project called jslinker. From what I gather, jslinker was a summer of code project from 2006 (the last commit seems to have been 2006, so it may be abandoned). I’m not sure what it’s capabilities are, but it’s at least proof that this can be done (or has been tried before).

† As search terms, link and javascript produce a lot of noise. You find a lot of results about hyperlinks, hrefs, html, and such things. I really look forward to the time when google figures out what to do with homonyms.



  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: