javascript var statement and performance

前端 未结 7 1514
后悔当初
后悔当初 2021-01-18 10:29

Option1 : multiple var without assignment

function MyFunction() {

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

  a = SomeValue;
         


        
相关标签:
7条回答
  • 2021-01-18 10:54

    They all basically do the same thing: declare a variable and eventually define it (assign something to it). The only thing you're changing throughout your examples is the order in which that occurs, and even that depends on the browser that is compiling your JavaScript -- some might perform optimizations that others do not.

    There are some stylistic guidelines you should follow though, and this answer explains them well. Keep in mind that that answer is technology agnostic and may not apply to JavaScript at all.

    0 讨论(0)
  • Just stay away from option 1 if possible, there is no need to make two assignments.

    Even variables declared inside a for loop or other compound statement/block will be available outside its containing block.

    You can also do:

    function MyFunction() {
    
      var a = SomeValue, b = SomeVavlue2;
    
    }
    
    0 讨论(0)
  • 2021-01-18 10:57

    "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...

    0 讨论(0)
  • 2021-01-18 11:00

    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.

    0 讨论(0)
  • 2021-01-18 11:01

    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?

    0 讨论(0)
  • 2021-01-18 11:01

    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) {
        var i,
            l;
        ...
        for (i = 0, l = arr.length; i < l; i++) {
            doStuff(arr[i]);
        }
        ...
    }
    
    0 讨论(0)
提交回复
热议问题