array.forEach running faster than native iteration? How?

前端 未结 5 849
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 19:38

http://jsperf.com/testing-foreach-vs-for-loop

It was my understanding that Test Case 2 should run more slowly than Test Case 1 -- I wanted to see how much more slowly. I

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-08 20:13

    Maybe the for() is slower because the loop applies 'array.length' to each iteration, to get the array's length.

    Try:

    var nri = array.length;
    for( var i = 0; i < nri; i++ ){
       // ...
    }
    

提交回复
热议问题