What is the best way to do loops in JavaScript

前端 未结 9 1852
温柔的废话
温柔的废话 2020-12-13 00:35

I have stumbled into several methods of looping in JavaScript, what I like the most is:

for(var i = 0; i < a.length; i++){
    var element = a[i];
}


        
相关标签:
9条回答
  • 2020-12-13 01:08

    I don't see what the problem with using a standard for(;;) loop is. A little test

    var x;
    var a = [];
    // filling array
    var t0 = new Date().getTime();
    for( var i = 0; i < 100000; i++ ) {
        a[i] = Math.floor( Math.random()*100000 );
    }
    
    // normal loop
    var t1 = new Date().getTime();
    for( var i = 0; i < 100000; i++ ) {
        x = a[i];
    }
    
    // using length
    var t2 = new Date().getTime();
    for( var i = 0; i < a.length; i++ ) {
        x = a[i];
    }
    
    // storing length (pollution - we now have a global l as well as an i )
    var t3 = new Date().getTime();
    for( var i = 0, l = a.length; i < l; i++ ) {
        x = a[i];
    }
    
    // for in
    var t4 = new Date().getTime();
    for( var i in a ) {
        x = a[i];
    }
    
    // checked for in
    var t5 = new Date().getTime();
    for( var i in a ) {
        if (a.hasOwnProperty(i)) {
            x = a[i];
        }
    }
    
    var t6 = new Date().getTime();
    var msg = 'filling array: '+(t1-t0)+'ms\n'+
              'normal loop: '+(t2-t1)+'ms\n'+
              'using length: '+(t3-t2)+'ms\n'+
              'storing length: '+(t4-t3)+'ms\n'+
              'for in: '+(t5-t4)+'ms\n'+
              'checked for in: '+(t6-t5)+'ms';
    console.log( msg );
    

    results in:

    filling array: 227ms
    normal loop: 21ms
    using length: 26ms
    storing length: 24ms 
    for in: 154ms
    checked for in: 176ms
    

    So:- for in's take the longest, using the length property (which is a property and doesn't need to be calculated) is nearly as fast as storing it first - which is only a whisker slower than using an integer.
    AND a for() is the usual way to loop over an array, which everyone expects and understands.

    All of them add a variable to the scope they run in - i - which is a common name for this use and so shouldn't be used for other things. Storing the length first adds another var - l - to the scope, which is unnecesary

    0 讨论(0)
  • 2020-12-13 01:10

    You could just always use a while loop, and compute the array limit before hand.

    Var max = a.length-1;
    var i = 0;
    
    while(i <= max)
    {
    var element = a[i];
    i++;
    }
    
    0 讨论(0)
  • 2020-12-13 01:14

    I know I'm late to the party, but I use reverse loops for loops that don't depend on the order.

    Very similar to @Mr. Muskrat's - but simplifying the test:

    var i = a.length, element = null;
    while (i--) {
      element = a[i];
    }
    
    0 讨论(0)
  • 2020-12-13 01:16

    Small improvement to the original, to only calculate the array size once:

    for(var i = 0, len = a.length; i < len; i++){ var element = a[i]; }
    

    Also, I see a lot of for..in loops. Though keep in mind that it's not technically kosher, and will cause problems with Prototype specifically:

    for (i in a) { var element = a[i]; }
    
    0 讨论(0)
  • 2020-12-13 01:19

    So, first you identify the perfect javascript loop, I believe it should look like this:

    ary.each(function() {$arguments[0]).remove();})

    This may require the prototype.js library.

    Next, you get disgustet with the arguments[0] part and have the code be produced automatically from your server framework. This works only if the ladder is Seaside.

    Now, you have the above generated by:

    ary do: [:each | each element remove].

    This comes complete with syntax completion and translates exactly to the above javascript. And it will make people's head spin that haven't used seasides prototype integration before, as they read your code. It sure makes you feel cool, too. Not to mention the gain in geekiness you can get here. The girls love it!

    0 讨论(0)
  • 2020-12-13 01:21

    I don't use it myself, but one of my colleagues uses this style:

    var myArray = [1,2,3,4];
    for (var i = 0, item; item = myArray[i]; ++i) {
        alert(item);
    }
    

    like Ash's answer, this will hit issues if you've got "falsey" values in your array. To avoid that problem change it to (item = myArray[i]) != undefined

    0 讨论(0)
提交回复
热议问题