Codewars Snail

JavaScript performance comparison

Revision 5 of this test case created by

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);
  }
  
  weizuo =function (arrayList) {
  	var result = [];
  	var point = {'x':0,'y':0};
  	var index = 0;
  	var last = arrayList.length * arrayList.length;
  	var deep = 0;
  	while (last > 0) {
  		result.push(arrayList[point.y][point.x]);
  		last -- ;
  		var change = turnArray[index](point,arrayList.length,deep);
  		if (change) {
  			index++;
  		}
  		if (4 == index) {
  			index = 0;
  			deep ++;
  		}
  	}
  	return result;
  }
  
  function goRight(point, size, deep) {
  	point.x ++;
  	return point.x == size - deep -1;
  }
  function goDown(point, size, deep) {
  	point.y ++;
  	
  	return point.y == size - deep -1;
  }
  function goLeft(point,size, deep) {
  	point.x --;
  	return point.x == deep;
  }
  function goUp(point,size, deep) {
  	point.y --;
  	return point.y == deep + 1;
  }
  
  var turnArray = [goRight,goDown,goLeft,goUp];
  
  jerryimba= 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 CCBot 2.0.0 / Other 0.0.0
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…
weizuo
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);
}

weizuo(x);
pending…
jerryimba
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);
}

jerryimba(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.

0 Comments