什么是扭转在Javascript数组的最有效方法是什么? [英] What is the most efficient way to reverse an array in Javascript?

查看:138
本文介绍了什么是扭转在Javascript数组的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我问什么是翻领在Javascript数组的最有效方式。此刻,我建议使用一个for循环,并与阵列摆弄,但后来意识到存在一个本机Array.reverse()方法。

I was asked recently what was the most efficient way to revers an array in Javascript. At the moment I suggested using a for loop and fiddling with the array but then realized there is a native Array.reverse() method.

有关好奇心的缘故,谁能帮助我通过展示例子或指向正确的方向,所以我可以读取到这个探索这个?任何关于如何meassure性能将是真棒,太建议。

For curiosity's sake, can anyone help me explore this by showing examples or pointing in the right direction so I can read into this? Any suggestions regarding how to meassure performance would be awesome too.

谢谢!

推荐答案

在此基础上设置:

var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var length = array.length;

Array.reverse(); 是第一或第二慢

该基准是在这里:
<一href=\"http://jsperf.com/js-array-reverse-vs-while-loop/5\">http://jsperf.com/js-array-reverse-vs-while-loop/5

跨浏览器,交换循环更快。有两种常见类型的交换算法(参见维基百科),每个有两个变化。

Across browsers, swap loops are faster. There are two common types of swap algorithms (see Wikipedia), each with two variations.

这两种类型的交换算法是临时交换和XOR交换。

The two types of swap algorithms are temporary swap and XOR swap.

这两个变化处理指数计算方式不同。在第一变化比较当前左索引和右索引,然后递减阵列右侧的索引。第二个变化比较当前左手食指和长度减半分,然后重新计算每次迭代的右手食指。

The two variations handle index calculations differently. The first variation compares the current left index and the right index and then decrements the right index of the array. The second variation compares the current left index and the length divided by half and then recalculates the right index for each iteration.

您可能会或可能不会看到两个版本之间的巨大差异。例如,在Chrome 18,临时交换和XOR交换的第一个变化比第二变化较慢的60%以上,但在Opera 12,临时交换和XOR互换的两种变化有类似表现。

You may or may not see huge differences between the two variations. For example, in Chrome 18, the first variations of the temporary swap and XOR swap are over 60% slower than the second variations, but in Opera 12, both variations of the temporary swap and XOR swap have similar performance.

临时交换:

第一变:

function temporarySwap(array)
{
    var left = null;
    var right = null;
    var length = array.length;
    for (left = 0, right = length - 1; left < right; left += 1, right -= 1)
    {
        var temporary = array[left];
        array[left] = array[right];
        array[right] = temporary;
    }
    return array;
}

第二个变化:

function temporarySwapHalf(array)
{
    var left = null;
    var right = null;
    var length = array.length;
    for (left = 0; left < length / 2; left += 1)
    {
        right = length - 1 - left;
        var temporary = array[left];
        array[left] = array[right];
        array[right] = temporary;
    }
    return array;
}


XOR交换:

第一变:

function xorSwap(array)
{
    var i = null;
    var r = null;
    var length = array.length;
    for (i = 0, r = length - 1; i < r; i += 1, r -= 1)
    {
        var left = array[i];
        var right = array[r];
        left ^= right;
        right ^= left;
        left ^= right;
        array[i] = left;
        array[r] = right;
    }
    return array;
}

第二个变化:

function xorSwapHalf(array)
{
    var i = null;
    var r = null;
    var length = array.length;
    for (i = 0; i < length / 2; i += 1)
    {
        r = length - 1 - i;
        var left = array[i];
        var right = array[r];
        left ^= right;
        right ^= left;
        left ^= right;
        array[i] = left;
        array[r] = right;
    }
    return array;
}


有一种叫解构掉分配的方法:
<一href=\"http://wiki.ecmascript.org/doku.php?id=harmony:destructuring\">http://wiki.ecmascript.org/doku.php?id=harmony:destructuring

解构赋值:

第一变:

function destructuringSwap(array)
{
    var left = null;
    var right = null;
    var length = array.length;
    for (left = 0, right = length - 1; left < right; left += 1, right -= 1)
    {
        [array[left], array[right]] = [array[right], array[left]];
    }
    return array;
}

第二个变化:

function destructuringSwapHalf(array)
{
    var left = null;
    var right = null;
    var length = array.length;
    for (left = 0; left < length / 2; left += 1)
    {
        right = length - 1 - left;
        [array[left], array[right]] = [array[right], array[left]];
    }
    return array;
}

现在,用解构分配算法是最慢的这一切。它比更慢Array.reverse(); 。但是,使用解构分配算法和 Array.reverse(); 方法是最短的例子,他们看起来干净。我希望自己的表现得到更好的未来。

Right now, an algorithm using destructuring assignment is the slowest of them all. It is even slower than Array.reverse();. However, the algorithms using destructuring assignments and Array.reverse(); methods are the shortest examples, and they look the cleanest. I hope their performance gets better in the future.

另外要提到的是,现代浏览器都改善阵列性能的拼接操作。

Another mention is that modern browsers are improving their performance of array push and splice operations.

在Firefox 10,使用数组这种循环算法拼接对手临时交换和XOR交换回路的算法。

In Firefox 10, this for loop algorithm using array push and splice rivals the temporary swap and XOR swap loop algorithms.

for (length -= 2; length > -1; length -= 1)
{
    array.push(array[length]);
    array.splice(length, 1);
}

不过,你应该与交换循环算法坚持,直到许多其他浏览器的匹配或超过他们的阵列拼接性能。

这篇关于什么是扭转在Javascript数组的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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