native reverse vs my reverse

JavaScript performance comparison

Test case created

Preparation code

<script>
  var a = [1, 2, 3, 4, 5];
 
  var reverse = function(arr) {
   var result = [],
       ii = arr.length;
   for (var i = ii - 1; i !== 0; i--) {
    result.push(arr[i]);
   }
   return result;
  }
 
  var reverse2 = function(arr) {
   var result = [],
       ii = arr.length,
       i = ii,
       j = 0;
   for (; i--;) {
    result[j++] = arr[i];
   }
   return result;
  }
 
  var reverse3 = function(arr) {
   var result = [],
       len = arr.length;
 
   for (var i = len - 1; i !== 0; i--) {
    result.push(arr[i]);
   }
 
   return result;
  }
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
native
var b = a.reverse();
pending…
mine
var b = reverse(a);
pending…
mine with second counter
var b = reverse2(a);
pending…
mine with more traditional loop
var b = reverse3(a);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment