array.forEach running faster than native iteration? How?

前端 未结 5 847
隐瞒了意图╮
隐瞒了意图╮ 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:34

    They're approximately the same for me in Opera. Something to note is that your conditional in the for() is array.length. If you cache the length of the array in a variable, and then loop, you should see better performance.

提交回复
热议问题