Why is iterating through an array backwards faster than forwards

后端 未结 7 1854
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 23:57

Given this code:

var arr = [];

for (var i = 0; i < 10000; ++i)
    arr.push(1);

Forwards

for (var i =          


        
相关标签:
7条回答
  • 2020-11-29 00:59

    Because in the first form you are accessing the property length of the array arr once for every iteration, whereas in the second you only do it once.

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