# Codewars Snail

## JavaScript performance comparison

Revision 12 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);
}

weizuo = function(arrayList) {
var result = [];
var x1 = 0;
var x2 = arrayList.length;
var y1 = 0;
var y2 = arrayList.length;
var i = 0;
var temp;
var deep = Math.ceil(x2/2);
while (deep) {
deep--;
for (i = x1; i < x2; i++) {
result.push(arrayList[y1][i]);
};
temp = x2 - 1;
for (i = y1+1; i < y2; i++) {
result.push(arrayList[i][temp]);
};
temp = y2 - 1;
for (i = x2-2; i >= x1; i--) {
result.push(arrayList[temp][i]);
};

for (i = y2-2; i > y1; i--) {
result.push(arrayList[i][x1]);
};
x1++;
x2--;
y1++;
y2--;
}
return result;
}
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;
}

liangbo = function snail(_array) {
var resultArray = new Array();
var index = 0;

function mySnail(_array) {
// 如果该数组只有一个元素
if (_array.length == 1) {
resultArray[index] = _array[0][0];
index++;
//console.log(_array[0][0]);
} else {
for (var i = 0; i < _array.length; i++) {
if (i == 0) {
// 第一行
for (var j = 0; j < _array[1].length; j++) {
resultArray[index] = _array[i][j];
index++;
//console.log(_array[i][j]);
}
} else {
// 最右列
resultArray[index] = _array[i][_array[i].length - 1];
index++;
//console.log(_array[i][_array[i].length - 1]);
// 最后一行
if (i == _array.length - 1) {
for (var j = _array[_array.length - 1].length - 2; j >= 0; j--) {
resultArray[index] = _array[i][j];
index++;
//console.log(_array[i][j]);
}
}
}
}
// 第一列
for (var i = _array.length - 2; i > 0; i--) {
resultArray[index] = _array[i][0];
index++;
//console.log(_array[i][0]);
}

// 如果原数组长度大于3
if (_array.length >= 3) {
var subArray = new Array(_array.length - 2);
for (var i = 0; i < subArray.length; i++) {
subArray[i] = new Array(_array.length - 2);
for (var j = 0; j < subArray[i].length; j++) {
subArray[i][j] = _array[i + 1][j + 1];
}
}
mySnail(subArray);
}
}
return resultArray;
}
mySnail(_array);
return resultArray;
}

real_liangbo = function snail(_array) {
var resultArray = new Array();

var x1 = 0;
var y1 = 0;

var x2 = _array.length - 1;
var y2 = 0;

var x3 = _array.length - 1;
var y3 = _array.length - 1;

var x4 = 0;
var y4 = _array.length - 1;

if (_array.length == 1) {
resultArray.push(_array[0]);
} else {
while (true) {
for (var i = x1; i < x2; i++) {
resultArray.push(_array[y1][i]);
}
for (var i = y2; i < y3; i++) {
resultArray.push(_array[i][x2]);
}
for (var i = x3; i > x4; i--) {
resultArray.push(_array[y3][i]);
}
for (var i = y4; i > y1; i--) {
resultArray.push(_array[i][x4]);
}

x1++;
y1++;

if (_array.length % 2 == 0 && y1 == y3) {
break;
}

x2--;
y2++;
x3--;
y3--;
x4++;
y4--;

if (_array.length % 2 != 0 && y1 == y3) {
resultArray.push(_array[y1][x1]);
break;
}
}
}
return resultArray;
}
};
</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…
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…
52liangbo
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);
}

liangbo(x);
pending…
real_liangbo
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);
}

real_liangbo(x);
pending…

## 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: