Take the 2-minute tour ×

This question relates to a previous question here:

Reducing number of calls to the methods of a JavaScript object

When profiling these two code snippets with Firebug:

function ie6PNGFixLoader(scriptURL) {
    if(arguments.length > 0) {
            $.ajax({// load PNG fix scripts
                url: arguments[i],
                cache: true,
                dataType: 'script'
    } else {
        return false;

var pngFix = "/Global/ICIS/Scripts/DD_belatedPNG_0.0.8a-min.js";    
var pngList = "/Global/ICIS/Scripts/DD_PNG_listing.js"; 
ie6PNGFixLoader(pngFix, pngList);


function InjectScriptsAndExecute(url) {
    this.url = url;

InjectScriptsAndExecute.prototype.InjectMethod = function() {
    var inject = $.ajax({
                        url: this.url,
                        cache: true,
                        dataType: 'script',
                        async: false, // Otherwise you cannot depend on the parse order
    return inject;  
var pngFix = new InjectScriptsAndExecute("/Global/ICIS/Scripts/DD_belatedPNG_0.0.8a-min.js");
var pngList = new InjectScriptsAndExecute("/Global/ICIS/Scripts/DD_PNG_listing.js");

It appers that the latter's calls to the InjectScriptsAndExecute method are much faster than the former's calls to its function. A colleague has asked me why when i mentioned the performance improvement but i cannot explain it myself.

Any advice for better understanding would be greatfully received.

share|improve this question
You're going to have to post more information about your performance measurements. The primary difference between those is not the coding style (which will make almost no measurable difference) but the differing setup of the ajax operations (async vs. synchronous). –  Pointy Jan 10 '11 at 14:40

1 Answer 1

up vote 6 down vote accepted

Arguments is not Array it's an Object that somewhat behaves like an array.

if(arguments.length > 0) { // Slow AND superfluous
    for (var i = 0; i < arguments.length; i++) { // Even SLOWER
        arguments[i]; // Holy...

Cache the length, accessing the property is slow, IE6 won't have no optimization at all for .length and I even suspect it to be really slow when using the arguments[i] since it is not a real Array and might therefore do an unoptimized property lookup.

If you want to get the best of both worlds, pass a normal Array, use a plain for loop, and cache the length.

function ie6PNGFixLoader(scripts) {
    for (var i = 0, l = scripts.length; i < l; i++) {
        $.ajax({// load PNG fix scripts
            url: scripts[i],
            cache: true,
            dataType: 'script'



To make it clear, timing the loop is useless, the request is async, all you do is timing a loop and a call to $.ajax there's no point in optimizing here, especially not for two entries. Even in IE6, doing the Ajax call itself (even just calling $.ajax) will be way slower then the loop.

share|improve this answer
@Anders so what, and why the downvote? Care to post more than a giant performance test that has not a word about arguments in it? –  Ivo Wetzel Jan 10 '11 at 15:03
@Anders And if you're complaining about the fact I did not recommend while(--i), and could have just as well ranted about premature optimization in my answer, and all I said was if you want the best of both words. Besides that, the loop is hardly the limiting factor here... the Ajax will take way more time. –  Ivo Wetzel Jan 10 '11 at 15:06
@Ivo Wetzel, I only posted a link about loop conditions and it's performance since you were talking about it. Thought it might be useful information for the OP. –  Anders Jan 10 '11 at 15:11
+1. While you can do convoluted things like while(--i) or for (i=0; node = array[i++];) and gain a tiny fraction of a micro-second, you lose the most important thing, readability. The for loop shown by @Ivo is clean, readable and understood by just about every single developer on the planet. There's no sense micro-optimizing that readability away unless it's absolutely positively necessary. And since the $.ajax call will dominate anyway, it's a moot point. –  ircmaxell Jan 10 '11 at 15:11

Your Answer


