How to do it more efficiently?

前端 未结 6 557
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 08:58

Let\'s imagine we should get some data...

var data = [];

//some code omitted that might fill in the data (though might not)

Then we need t

6条回答
  •  醉话见心
    2021-01-06 09:03

    I believe the second version is more idiomatic, not just in JavaScript, but in most programming languages. If you're that concerned about timing, you could save the length of the array in a variable, and use that variable in the loop, but I don't think that's necessary:

    var data = [];
    var length = data.length;
    for (var i = 0; i < length; i++) {
        //iterate over the data and do something to it
    }
    

提交回复
热议问题