1

lets say that I have the following scenario:

var namespace = {};
(function($)
{
    $.extend(namespace, 
    {
        test1: function(someArray, someObj)
        {
             for(var i= 0, ii= someArray.length;i<ii;i++)
             {
                 var text = someObj[someArray[i]];
                 // do something with text
             }
        },
        test2: function(someArray, someObj,i,ii,text)
        /*
             see that the i,ii,text are  unused parameters,
             that will be used instead of variables
        */
        {
             for(i= 0, ii= someArray.length;i<ii;i++)
             {
                  text = someObj[someArray[i]];
                 // do something with text
             }
        },
    });
})(jQuery);

Now, the result of the test1 and test2 are the same... but what about the performance, memory usage... Is there any difference between declaring the i,ii, test variables in the two ways presented above ?

I think that the test2, for example, is probably more efficient because the variables are in the local function scope so after the function exits, the execution context is destroy, releasing the resources used for the arguments... the variables will not be assigned to the global object 'window'.

So what method is performing best? and why?


[Edit]

Thanks all for your answers !

There is no problem if the code has readability issues... I`m only interested now about the performance/memory usage.

Alex Pacurar
  • 5,801
  • 4
  • 26
  • 33

5 Answers5

1

If you do not declare your variables with var i then they become implicitly global.

Always declare your variables. If you did any benchmarking on that you would find that declared local variables are actually faster then implied global variables. Also you don't leak to the global state that way.

Benchmark!.

As you can see the performance is identical.

In terms of memory usage, local variables (test1) are probably better as the compiler doesn't have to remember that the function has 5 parameters.

But that's a nano optimisation If you care about performance differences of this caliber write assembly instead. Go for readable and maintanable code.

[Edit]

Didn't notice "local" variables in method parameter. That is a readability killer! Don't do that. You will find that test1 is probably still more efficient.

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • I don't disagree but note that in "test2" the identifiers are listed out as (presumably unused) parameters to the function. – Pointy Mar 04 '11 at 13:35
  • @Pointy As mentioned I didn't notice. That's a horrible readability point. – Raynos Mar 04 '11 at 13:38
  • @Martijn that's impressive. Must be a bug in the FF4beta interpreter that causes function parameters to be very expensive storage. – Raynos Mar 04 '11 at 13:55
  • Yes, it’s very strange. I’ve submitted it to Mozilla, I hope someone there can shed some light on it. – Martijn Mar 04 '11 at 14:13
1
  1. Why don't you profile your code?
  2. The variables are also local in test1. You are declaring them with var i. There is no difference between these methods.
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

The variables in "test1" are all declared with var, so they're not global. Those two should be essentially the same.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

test1 is faster, because every time JavaScript looks for symbols (such as a variable name) it starts by looking in the local scope. So by using global scope it has to look in more places to find the symbols. The same applies for parameters, but they are better than globals.

Simeon
  • 5,519
  • 3
  • 29
  • 51
0

It may be miniscule, but declaring a new variable (text) in every iteration would require new memory allocation I believe. Though I'm not sure how javascript handles that. I usually declare variables beforehand and then assign values afterwards for that reason, but that is only because someone said "hey you should do it that way" and presented the same argument.

MxLDevs
  • 19,048
  • 36
  • 123
  • 194