Javascript 为什么向后迭代数组比向前迭代快

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8689573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:05:12  来源:igfitidea点击:

Why is iterating through an array backwards faster than forwards

javascriptperformanceoptimization

提问by samccone

Given this code:

鉴于此代码:

var arr = [];

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

Forwards

远期

for (var i = 0; i < arr.length; ++i) {}

Backwards

向后

for (var i = arr.length - 1; i >= 0; --i) {}

Hard-coded Forward

硬编码转发

for (var i = 0; i < 10000; ++i) {}

Why is backwards so much faster?

为什么向后要快得多?

Here is the test: http://jsperf.com/array-iteration-direction

这是测试:http: //jsperf.com/array-iteration-direction

回答by Bergi

Because your forwards-condition has to receive the lengthproperty of your array each time, whilst the other condition only has to check for "greater then zero", a very fast task.

因为您的转发条件length每次都必须接收数组的属性,而另一个条件只需要检查“大于零”,这是一项非常快的任务。

When your array length doesn't change during the loop, and you really look at ns-perfomance, you can use

当你的数组长度在循环过程中没有改变,并且你真的查看了 ns-perfomance 时,你可以使用

for (var i=0, l=arr.length; i<l; i++)

BTW: Instead of for (var i = arr.length; i > 0; --i)you might use for (var i = arr.length; i-- > 0; )which really runs through your array from n-1 to 0, not from n to 1.

顺便说一句:而不是for (var i = arr.length; i > 0; --i)您可能使用for (var i = arr.length; i-- > 0; )which 真正从 n-1 到 0,而不是从 n 到 1 遍历您的数组。

回答by rabusmar

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

因为在第一种形式中,您每次迭代都会访问一次length数组的属性arr,而在第二种形式中,您只访问一次。

回答by tcak

If you want to have them at same pace, you can do that for forward iteration;

如果你想让它们以相同的速度运行,你可以在前向迭代中这样做;

for(var i=0, c=arr.length; i<c; i++){
}

So, your script won't need to take length of array on everystep.

因此,您的脚本不需要在每一步都占用数组的长度。

回答by Gopal Nair

I am not entirely sure about this, but here is my guess:

我对此并不完全确定,但这是我的猜测:

For the following code:

对于以下代码:

for (var i = 0; i < arr.length; ++i) {;
}

During runtime, there is an arr.length calculation after each loop pass. This may be a trivial operation when it stands alone, but may have an impact when it comes to multiple/huge arrays. Can you try the following:

在运行时,每次循环通过后都会进行 arr.length 计算。这在单独使用时可能是一个微不足道的操作,但在涉及多个/大型阵列时可能会产生影响。您可以尝试以下操作:

 var numItems = arr.length;
    for(var i=0; i< numItems; ++i)
    {
    }

In the above code, we compute the array length just once, and operate with that computed number, rather than performing the length computation over and over again.

在上面的代码中,我们只计算一次数组长度,并使用计算出的数字进行操作,而不是一遍又一遍地执行长度计算。

Again, just putting my thoughts out here. Interesting observation indeed!

再次,只是把我的想法放在这里。确实有趣的观察!

回答by jfriend00

i > 0is faster than i < arr.lengthand is occurring on each iteration of the loop.

i > 0i < arr.length循环的每次迭代更快并且正在发生。

You can mitigate the difference with this:

您可以通过以下方式减轻差异:

for (var i = 0, len = arr.length; i < len; ++i) {;
}

This is still not as fast as the backwards item, but faster than your forward option.

这仍然不如向后项目快,但比向前选项快。

回答by kennebec

And these are equally good:

这些也同样好:

var arr= [], L= 10000;
while(L>-1) arr[L]= L--;

OR

或者

var arr= [], i= 0;
while(i<10001) arr[i]=i++;

回答by dku.rajkumar

do it like below, it will perform in same way. because arr.lengthtakes time in each iteration in forward.

像下面那样做,它会以同样的方式执行。因为arr.length在向前的每次迭代中都需要时间。

int len = arr.length;

forward

向前

for (var i = 0; i < len; ++i) {
}

backward

落后

for (var i = len; i > 0; --i) {
}