Based on this setting:
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var length = array.length;
Array.reverse(); is the first or second slowest!
Tests here: http://jsperf.com/js-array-reverse-vs-while-loop/5
In all browsers, swap circuits are faster. There are two general types of swap algorithms (see Wikipedia ), each of which has two options.
The two types of subcategories are temporary swap and XOR summary.
Two options handle index calculations differently. The first variation compares the current left index and right index, and then decreases 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.
You may or may not see huge differences between the two options. For example, in Chrome 18, the first options for temporary swap and XOR replacement are 60% slower than in the second option, but in Opera 12, both options for temporary swap and XOR exchange have similar performance.
Temporary swap:
The first variation:
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; }
Second variation:
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 swap:
The first variation:
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; }
Second variation:
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; }
There is another swap method called destructuring assignment: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring
Destructuring purpose:
The first variation:
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; }
Second variation:
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; }
Currently, an algorithm using a destructuring assignment is the slowest of all. It is even slower than Array.reverse(); . However, algorithms using destructuring assignments and Array.reverse(); methods Array.reverse(); are the shortest examples and they look cleaner. I hope their performance improves in the future.
Another mention that modern browsers improve the performance of push and splice arrays.
In Firefox 10, this for loop algorithm using push and splice arrays competes with temporary swap and XOR swap algorithms.
for (length -= 2; length > -1; length -= 1) { array.push(array[length]); array.splice(length, 1); }
However, you should probably stick to the swap loop algorithms until many of the other browsers match or outperform your push and splice array.