Skip to content Skip to sidebar Skip to footer

Javascript Var Statement And Performance

Option1 : multiple var without assignment function MyFunction() { var a = null; var b = null; .... var z = null; a = SomeValue; b = SomeValue2; .... } Option 2: on

Solution 1:

"premature optimization is the root of all evil"

I don't think there will be any significant performance change with any of this options. (IMO) The third option is the most readable option and closest to dynamic memory allocation like C# etc'. But this is my humble opinion, Choose what you like the most.

If it really bothers you and you can't sleep without an answer, test it with jsPerf.


@Chad made a jsPerf so you can sleep well tonight...

Solution 2:

To understand the performance you should first understand hoisting. Let's take the following code:

var x = 1;

function bar(val) {
    var returnVal = val * 2;

    return returnVal;
}

function foo(val) {
    var returnVal = 10;

    returnVal *= bar(val);

    return returnVal;
}

var y = foo(x);

console.log(y); // 20

Hoisting basically means that the JavaScript interpreter will 'hoist' the variable declaration to the top of its scope. Making this example look more like this:

var x, y;

x = 1;

function bar(val) {
    var returnVal;

    returnVal = val * 2;

    return returnVal;
}

function foo(val) {
    var returnVal;

    returnVal = 10;
    returnVal *= bar(val);

    return returnVal;
}

y = foo(x);

console.log(y); // 20

So, in your examples given, Option 2 and 3 will basically do the same thing. Since the interpreter will move those declarations to the top. At that point it's a decision of preference. A lot of people avoid doing something like var x, y, z; saying it's dangerous. I, personally, do it. In whatever scope I'm in I will declare all variables at the top, and then use them below. But either way works.

Now, your first example is the least efficient. After hoisting it will look like this:

function MyFunction() {
    var a, b, ... z;

    a = null;
    b = null;
    ...
    z = null;

    a = someValue;
    b = someValue2;
    ...
    z = someValueN;
}

It basically results in setting the variables twice.

Solution 3:

In Chrome, execution times are identical.

The only real consideration here is optimizing network transfer.

Consider var a=1;var b=2;...;var z=9; versus var a=1,b=2,...,z=9;

If you put a ;var  in front of each identifier, that's 5 bytes (assuming a single-byte character encoding), versus 1 byte for a ,. Thus, declaring 26 variables, you can save 100 bytes by writing var  once and listing identifiers with commas.

Granted it's not a huge savings, but every byte helps when it comes to pushing bits over the network. It's not something to worry a great deal about, but if you find yourself declaring several variables in the same area, using the variable declaration list is an easy way to shave a few bytes off your JS file.

Solution 4:

That seems like premature optimization, the root of all evil.

How many times will it be executed? What fraction of of the whole program will this consume?

Solution 5:

I will counter your question about performance with a few questions:

  1. Have you noticed an issue with performance?
  2. Have you determined that your var statement was the bottleneck?
  3. Have you tested each version?

I'm going to assume the answer to all of these was no.

None of the options should make any significant difference, although the only way to know for certain is to run them and test. JavaScript being an asynchronous client-side language means that you'd have to run a seriously enormous amount of code to have the var statements be a bottleneck of any sort.


In the end, if you're deciding which version to use, I would recommend using a single var statement without assignment:

function () {
    var a,
        b,
        c,
        foo,
        bar,
        fizz,
        buzz;
    a = 1;
    ...
    b = 3;
}

The reason for this is that this is essentially how the code will be executed due to variable hoisting. I can then move the initialize statement so where the variable is first used, such as in a for loop:

function foo(arr) {
    vari,
        l;
    ...
    for (i = 0, l = arr.length; i < l; i++) {
        doStuff(arr[i]);
    }
    ...
}

Post a Comment for "Javascript Var Statement And Performance"