Codewars Snail

JavaScript performance comparison

Revision 9 of this test case created by real_liangbo

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);
    }
};
</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…

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