JavaScript循环性能 - 为什么将迭代器减少到比递增更快的速度 [英] JavaScript loop performance - Why is to decrement the iterator toward 0 faster than incrementing

查看:115
本文介绍了JavaScript循环性能 - 为什么将迭代器减少到比递增更快的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在他的书中更快的网站 Steve Sounders写道,提高循环性能的一种简单方法是将迭代器递减到0而不是递增到总长度(实际上章节是由Nicholas C. Zakas编写的)。根据每次迭代的复杂性,此更改可以比原始执行时间节省高达50%的成本。例如:

In his book Even Faster Web Sites Steve Sounders writes that a simple way to improve the performance of a loop is to decrement the iterator toward 0 rather than incrementing toward the total length (actually the chapter was written by Nicholas C. Zakas). This change can result in savings of up to 50% off the original execution time, depending on the complexity of each iteration. For example:

var values = [1,2,3,4,5];
var length = values.length;

for (var i=length; i--;) {
   process(values[i]);
}

对于 几乎相同c $ c>循环, do-while 循环, while 循环。

This is nearly identical for the for loop, the do-while loop, and the while loop.

我想知道,这是什么原因? 为什么要以更快的速度递减迭代器?(我对此技术背景感兴趣,而不是在证明此声明的基准测试中感兴趣。)

I'm wondering, what's the reason for this? Why is to decrement the iterator so much faster? (I'm interested in the technical background of this and not in benchmarks proving this claim.)

编辑:乍一看,这里使用的循环语法看起来不对。没有 length-1 i> = 0 ,所以让我们澄清一下(我也很困惑)。

At first sight the loop syntax used here looks wrong. There is no length-1 or i>=0, so let's clarify (I was confused too).

以下是循环语法的一般说明:

Here is the general for loop syntax:

for ([initial-expression]; [condition]; [final-expression])
   statement




  • initial-expression - var i = length

    首先评估此变量声明。

    This variable declaration is evaluated first.

    条件 - i -

    在每次循环迭代之前计算此表达式。它将在第一次通过循环之前递减变量。如果此表达式的计算结果为 false ,则循环结束。在JavaScript中 0 == false 所以如果 i 最后等于 0 它被解释为 false 并且循环结束。

    This expression is evaluated before each loop iteration. It will decrement the variable before the first pass through the loop. If this expression evaluates to false the loop ends. In JavaScript is 0 == false so if i finally equals 0 it is interpreted as false and the loop ends.

    final-expression

    此表达式在每次循环迭代结束时(在条件的下一次评估之前)进行评估。这里不需要它是空的。所有三个表达式在for循环中都是可选的。

    This expression is evaluated at the end of each loop iteration (before the next evaluation of condition). It's not needed here and is empty. All three expressions are optional in a for loop.

    for循环语法不是问题的一部分,而是因为它有点不常见我觉得澄清它很有意思。也许一个原因是它更快,因为它使用较少的表达式( 0 == false 技巧)。

    The for loop syntax is not part of the question, but because it's a little bit uncommon I think it's interesting to clarify it. And maybe one reason it's faster is, because it uses less expressions (the 0 == false "trick").

    推荐答案

    我不确定Javascript,在现代编译器下它可能没关系,但在过去的日子这段代码:

    I'm not sure about Javascript, and under modern compilers it probably doesn't matter, but in the "olden days" this code:

    for (i = 0; i < n; i++){
      .. body..
    }
    

    将生成

    move register, 0
    L1:
    compare register, n
    jump-if-greater-or-equal L2
    -- body ..
    increment register
    jump L1
    L2:
    

    而向后计数代码

    for (i = n; --i>=0;){
      .. body ..
    }
    

    将生成

    move register, n
    L1:
    decrement-and-jump-if-negative register, L2
    .. body ..
    jump L1
    L2:
    

    所以在循环中它只做两个分机ra指令而不是四个。

    so inside the loop it's only doing two extra instructions instead of four.

    这篇关于JavaScript循环性能 - 为什么将迭代器减少到比递增更快的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆