Improve Javascript-Load Speeds

I like how Google service pages load up faster than others. Most of it can be attributed to their minimalistic design, but if you ever open up Firebug’s console, you’ll see the actual reason.

You have to love asynchronous loading

The good thing about Javascript is that it’s multi-threaded — as far as asynchronous calls are concerned at least. When you make an XMLHTTPRequest call to external files, it forgets about the process and continues while the content is loaded in the background. I’m going to call out Deepak on this, because even though his method is good, the problem is what he points out himself. Different browsers handle the loading differently. Also, it involves DOM manipulation, which restricts your script to the speed of (X)HTML.

With an asynchronous call, we are bootstrapping our scripts. It allows them to load nicely in the background, while the browser continues to render the markup. One they’re done, they will always be executed in the sequence they are loaded. Gives you at least one constant to work with. If your scripts are large, they will take longer to load. Calling functions that are yet to be loaded is always dangerous, which is why you should use fallback mechanisms.


A call to eval() with the Javascript file’s content passed as a string will result in it being ready for use on the current page. You will have to rewrite your code to JSON so that the evaluation doesn’t throw errors, which will not take too much time. Using the jQuery framework, the code can simply be:

$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "futureload.js",
        data: "ver=1.0",
        success: function(content){
                eval('js ='+content);
        }
    });
});

And the corresponding Javascript file could be:

{
    alert1: function(){
        alert('Greetings from "alert1"');
    },

    alert2: function(){
        alert('Greetings from "alert2"');
    }
}

Then call js.function-name() to display the required alert. Using the same skeleton, you can complicate your Javascript file while not losing out on page load times.

Your mileage might vary, and this is definitely the wrong way to make something right. But we’re all for hacks, aren’t we?


2 Comments

gravatar

Nice work. As I have agreed before, fallback mechanisms and error handling are essential in any code we write. The file being ready is only one part. Many times we have inter-dependencies between two javascript files. That’s where the problem starts. I haven’t given much thought to that even if some people commented about this issue. You know I’m lazy. :P

That said, yours is a cool way of doing the eval.

PS: OpenID again started to fail for me here. After validation in my claimID, it returns back here and says “Incorrect Password”!!! What the hell is that supposed to mean? :o

gravatar

Many times we have inter-dependencies between two javascript files.

This method takes care of that too. Suppose your custom JS-file is dependent on jQuery to work. In the onSuccess handler of your call to the custom script file, put in a check to see if jQuery exists. If it doesn’t, fire a setTimeout to check again after x seconds, and then evaluate the script as required.

That is the ‘awesome’ factor of bootstrapping your scripts. You can use script-level conditions to make sure everything works exactly the way you want it, and isn’t left to network speeds, browser dependencies etc.. Too many variables spoil the broth.


Leave a comment

RSS feed for comments on this post. TrackBack URL

Copyright © 2006-08 Aditya Mukherjee | Valid XHTML 1.0 Transitional Valid CSS!