Codewars Snail

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    stevenbarragan = function(array) {
      return array.length == 0 || array.length == 1 ? array : [].concat(
                array.shift(),
                array.map(function(_,i){return array[i].pop();}),
                array.pop().reverse(),
                array.map(function(_,i){return array[i].shift();}).reverse(),
                stevenbarragan(array)
        );
    }
   
    haan = function(n) {
      var i_snail = function(n, x, y) {
        var result = [];
        for (var i = x; i <= y; i++) {
          result.push(n[x][i]);
        }
        for (var i = x+1; i <= y; i++) {
          result.push(n[i][y]);
        }
        for (var i = y-1; i >= x; i--) {
          result.push(n[y][i]);
        }
        for (var i = y-1; i >= x+1; i--) {
          result.push(n[i][x]);
        }
        if (x < y) {
          return result.concat(i_snail(n, x+1, y-1));
        } else {
          return result;
        }
      }
      return i_snail(n, 0, n[0].length - 1);
    }
    snail = function(array) {
      var opt = [];
      for(var i = 0,l=Math.ceil(array.length/2);i<l;i++){
        opt = opt.concat(array.shift());
        for(var j = 0;j<array.length-1;j++){
          opt.push(array[j].pop());
   
        }
   
        array.length >0?opt = opt.concat(array.pop().reverse()):0;
        for(var j = array.length-1;j>=0;j--){
   
          opt.push(array[j].shift());
        }
      }
      return opt;
    }
};
</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
stevenbarragan
var x = [];
for(var i = 0; i < 100; i++) {
  x[i] = [];
  for(var k = 0; k < 100; k++)
    x[i][k] = Math.floor(Math.random() * 10);
}

stevenbarragan(x);
pending…
haan
var x = [];
for(var i = 0; i < 100; i++) {
  x[i] = [];
  for(var k = 0; k < 100; k++)
    x[i][k] = Math.floor(Math.random() * 10);
}

haan(x);
pending…
me
var x = [];
for(var i = 0; i < 100; i++) {
  x[i] = [];
  for(var k = 0; k < 100; k++)
    x[i][k] = Math.floor(Math.random() * 10);
}

snail(x);
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