简体   繁体   English

谁能解释一下这两个循环之间的区别?

[英]Can anyone explain me the difference between these two loopings?

http://jsperf.com/jquery-each-vs-for-loop/108 http://jsperf.com/jquery-each-vs-for-loop/108

for (var b = a[0], len = a.length; len; b = a[--len]) {
  newArray.push(
    b
  );
}

and

for (var i = 0, len = a.length; i < len; i++) {
  newArray.push(
    a[i]
  );
}
  1. According to jsref, it says the first one is faster. 根据jsref,它说第一个更快。 why? 为什么?
  2. Can anyone explain me the for loop on whats its doing compared to traditional way? 任何人都能解释一下for循环吗?与传统方式相比,它的做法是什么?

Your first example just does something very different. 你的第一个例子做了一些非常不同的事情 Check it out: 看看这个:

var a = [1,2,3,4],
    newArray = [];
for (var b = a[0], len = a.length; len; b = a[--len]) {
    newArray.push(b);
}

> newArray
[1, 4, 3, 2]

Your second example results in the expected [1, 2, 3, 4] . 你的第二个例子产生了预期的[1, 2, 3, 4]


If you need to understand the algorithms, it might be easier when converting the for -notation to while-loops and to expand the decrement and increment operators: 如果您需要了解算法,将for -notation转换for while循环并扩展递减和递增运算符可能会更容易:

/* first example */
var b = a[0],
    len = a.length;
while (len) {
    newArray.push(b);
    len = len-1;
    b = a[len];
}

/* second example */
var i = 0,
    len = a.length;
while (i < len) {
    newArray.push( a[i] );
    i = i+1;
}

I think the major gain of the first loop is that a is being cached in len and then used as a single boolean value : there's a little time to be gained by simplifying the loop condition. 我认为第一个循环的主要好处是a被缓存在len ,然后用作单个布尔值:通过简化循环条件可以获得一点时间。 However! 然而! It will not produce the same array as output. 它不会产生与输出相同的数组。

Assume you have ten elements in a : [0,1,2,3,4,5,6,7,8,9] -- the loop will process a[0] first, then a[--len], which is a[9] in our example. 假设你有一个元素:[0,1,2,3,4,5,6,7,8,9] - 循环首先处理[0],然后处理[ - len],在我们的例子中是[9]。 Your end result will be newArray = [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]. 你的最终结果将是newArray = [0,9,8,7,6,5,4,3,2,1]。

So it doesn't matter why it's faster, because it's doing the wrong thing. 因此,为什么它更快更重要,因为它做错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能解释这两个代码之间的区别? 又为什么第二个有效而第一个无效呢? - Can anyone explain the difference between those two codes? And why does the second one work and the first doesn't? 任何人都可以解释闭包和匿名函数之间的区别吗? - Can anyone explain the difference between closure and anonymous functions? 谁能解释一下 Reacts 单向数据绑定和 Angular 的双向数据绑定的区别 - Can anyone explain the difference between Reacts one-way data binding and Angular's two-way data binding 任何人都可以向我解释我的代码中返回与警报功能之间的区别是什么 - Could anyone please explain to me what is the difference between return vs alert function in my code 谁能向我解释嵌套的每种行为? - Can anyone explain this nested each behavior to me? 任何人都可以向我解释document.cookie - Anyone can explain to me document.cookie 谁能向我解释这个jQuery插件? - Can anyone explain this jQuery plugin to me? 任何人都可以向我解释这个减少代码吗? - Can anyone pls explain this reduce code to me? 谁能解释一下jquery中函数的执行过程 - Can anyone explain me the execution process of functions in jquery 谁能解释一下“ if(blnRtrn == true)”的工作原理吗? - Can anyone please explain me how this “if (blnRtrn == true)” works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM