JS: Array.reverse() vs while loop (v27)

Revision 27 of this benchmark created by Simon Karman 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
backwardI = len - 1;
while (backwardI > -1) {
  ar.push(ar[backwardI]);
  ar.splice(backwardI, 1);
  backwardI--;
}
ready
Forward while loop
forwardI = 0;
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.