JS: Array.reverse() vs while loop

Benchmark created by Matt McDonald on


Description

Checking performance of Array.reverse() vs a quick while loop.

Preparation HTML

<script>
  var ar = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  var len = ar.length;
  var backwardI = len - 1;
  var forwardI = 0;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Array.reverse()
ar.reverse();
ready
Backward while loop
while (backwardI > -1) {
 ar.push(ar[backwardI]);
 ar.splice(backwardI, 1);
 backwardI--;
}
ready
Forward while loop
while (forwardI < len) {
 var end = (len - 1) - forwardI;
 ar.push(ar[end]);
 ar.splice(end, 1);
 forwardI++;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.