Quick trimmed-down test: [].reverse() vs. crazy push(), splice() loop vs. another loop, each as a generic func (v58)

Revision 58 of this benchmark created by SirShanus on


Description

(This isn't a particularly useful revision -- just grabbing some ready-made logic for a quick check of some of the better candidates...)

Preparation HTML

<script>
  var   tArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        arrReverse = tArr.reverse;

  function bizarroArrReverse(arr) {
    for ( var idx = (arr.length - 2); idx >= 0; idx-- ) {
      arr.push(arr[idx]);
      arr.splice(idx, 1);
    }
    return arr;
  }

  function swapLoopArrReverse(arr) {
    var   lIdx = 0,   rIdx = (arr.length - 1),   tmp;
    for (lIdx, rIdx; lIdx < rIdx; lIdx++, rIdx--) {
      tmp = arr[lIdx];
      arr[lIdx] = arr[rIdx];
      arr[rIdx] = tmp;
    }
    return arr;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Array.proto.reverse()
/*
Since the focus here is on possible util methods that might conceivably be called on one or another weirdo arrLikeObjs, we gotta keep things generic...
*/
// ~ Equivalent to `[].reverse.call(tArr)` ~

arrReverse.call(tArr);
ready
for push() then splice()
// ~ Ops performed by called function ~
// for (length -= 2; length > -1; length -= 1) {
//   array.push(array[length]);
//   array.splice(length, 1);
// }

bizarroArrReverse(tArr);
ready
simple for swap
// ~ Ops performed by called function ~
// var   left = 0,   right = length - 1;
// for (left, right; left < right; left += 1, right -= 1) {
//   var temporary = array[left];
//   array[left] = array[right];
//   array[right] = temporary;
// }

swapLoopArrReverse(tArr);
ready

Revisions

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