JS: Array.reverse() vs. for and while loops (v43)

Revision 43 of this benchmark created by Hugo Villeneuve on


Description

Based on version 8 of this test. No improvements over the initial code except for the 2 variable loop alternatives that show some difference on how browsers optimise them.

Comments from the original post (case #8):

Checking performance of Array.reverse() vs custom loops.

See http://stackoverflow.com/questions/5276953/what-is-the-most-efficient-way-to-reverse-an-array-in-javascript.

Also see http://jsperf.com/forloop-vs-reverse-while-loop-and-array-reverse/9.

Edited to make sure JIT isn't being "smart" and doing some extra optimizations that won't exist on real applications, also increased the number of items to 1000 to represent a common scenario (perf usually changes drastically based on amount of items, some techniques might only be fast for a short amount of items). See: http://blog.millermedeiros.com/benchmarking-is-hard/

Setup

// at least 1K items to make sure it's fast, performnace usually changes
    // based on the amount of items
    var array = [];
    var n = 1000;
    while (n--) {
      array.push(n * Math.random());
    }
    
    // we store result outside of test scope to make sure JIT isn't simply ignoring
    // the operations on some cases
    var result;
    
    
    
    function forSwap(array){
      var length = array.length;
      var left = null;
      var right = null;
      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 forSwapHalf(array){
      var length = array.length;
      var left = null;
      var right = null;
      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;
    }
    
    function forSwap2(a){
      for (var i=0, j=a.length - 1, t; i < j; i++, j--) {
          t = a[i];
          a[i] = a[j];
          a[j] = t;
      }
      return array;
    }
    
    function whileSwap(a){
      var i = 0, j = a.length - 1, t
      while (i < j) {
          t = a[i];
          a[i++] = a[j];
          a[j--] = t;
      }
      return a;
    }
    
    
    function forSwapHalf2(array){
      var length = array.length;
      for (var i=0, j=length-1, t; i < length / 2; i++, j--) {
        t = array[i];
        array[i] = array[j];
        array[j] = t;
      }
      return array;
    }
    
    function forSwapHalf3(array){
      var length = array.length;
      for (var i=0, j=length-1, t; i < length / 2; i++) {
        t = array[i];
        array[i] = array[j];
        array[j--] = t;
      }
      return array;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Array.reverse()
result = array.reverse();
ready
for swap
result = forSwap(array);
ready
for swap half
result = forSwapHalf(array);
ready
for swap tweek
result = forSwap2(array);
ready
while swap
result = whileSwap(array);
ready
for swap half tweek
result = forSwapHalf2(array);
ready
for swap half tweek 2
result = forSwapHalf3(array);
ready

Revisions

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