简体   繁体   English

这两个for循环之间的区别

[英]Difference between these two for loops

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

}

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

}
  1. Does both of them does the same job or the first one does things differently. 他们两个都做相同的工作,还是第一个做不同的事情。
  2. Which is the best way to perform compared to the above two. 与以上两种相比,这是最佳的执行方式。

First one will start from bottom of array and will reach top. 第一个将从数组的底部开始,然后到达数组的顶部。 Second one will start from top to bottom of array. 第二个将从阵列的顶部到底部开始。

If your array has { 3, 2, 1 } and you print this in first loop, it will print 1, 2, 3 and in second loop it will print 3, 2, 1. 如果您的数组具有{3,2,1},并且您在第一个循环中打印该数组,它将打印1,2,3,在第二个循环中它将打印3,2,1。

Found this http://oreilly.com/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html which tells about improving performance of javascript. 找到了这个http://oreilly.com/server-administration/excerpts/even-faster-websites/writing-efficiency-javascript.html ,其中介绍了如何提高javascript的性能。 According to it 根据它

Another simple way to improve the performance of a loop is to decrement the iterator toward 0 rather than incrementing toward the total length. 改善循环性能的另一种简单方法是将迭代器减0,而不是增加总长度。 Making this simple change can result in savings of up to 50% off the original execution time, depending on the complexity of each iteration. 进行此简单更改可以节省多达50%的原始执行时间,具体取决于每次迭代的复杂程度。

So first one will give better performance . 因此, 第一个将提供更好的性能

Hope this helps you. 希望这对您有所帮助。

The first loops backwards. 第一个循环向后循环。 The second loops forwards. 第二个循环向前。

The first will have better performance (because the second has to access the arr.length each time it goes around the loop), but not significantly so unless you are dealing with a lot of objects or looping over them many times. 第一个将具有更好的性能(因为第二个必须在每次循环时都访问arr.length ),但不会显着提高,除非您要处理大量对象或对其进行多次循环。

You can get a similar performance boost with: 您可以通过以下方式获得类似的性能提升:

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

}

The first one loops through the array in reverse. 第一个循环反向遍历数组。 The second one in the order the elements appear. 元素显示顺序中的第二个。

The two loops are different in the direction of an array traversal: 这两个循环在数组遍历的方向上是不同的:

  1. starts from the last element and finishes in the first element 从最后一个元素开始,在第一个元素中结束
  2. vice versa, starts from the fist element and finishes in the last one 反之亦然,从拳头元素开始,到最后一个元素结束

Which is the best, depends on your needs. 哪个最好,取决于您的需求。

Consider, for example, the task of finding the first/last occurrence of a character in an array. 例如,考虑在数组中查找字符的第一个/最后一个出现的任务。

They both iterate over the contents of an array, but they do it in opposite directions. 它们都遍历数组的内容,但方向相反。 The first starts from the last element of the array, and works backwards to the first element; 第一个从数组的最后一个元素开始,然后向后移动到第一个元素。 the second starts from the first element, and works forwards to the last. 第二个从第一个元素开始,一直到最后一个。

In most cases, they'll provide the same results. 在大多数情况下,它们将提供相同的结果。 However, it's a common practice when you're going to be removing elements from an array to iterate backwards (the first code sample), as any changes to the indices after removing an element will only affect the elements that have already been examined. 但是,当您要从数组中删除元素以向后迭代时(第一个代码示例),这是一种常见的做法,因为删除元素后对索引的任何更改只会影响已经检查过的元素。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM