# sku组合算法性能测试

## JavaScript performance comparison

Revision 12 of this test case created by

## Preparation code

``````

<script>
Benchmark.prototype.setup = function() {
var saleProp = [
['A', 'B', 'C', 's', 's', 'as', 'ad', 'sas', 'as', 'sa', 'asd', 'asd', 'asd', 'asdf', 'asdf'],
['D', 'E', 'F', 'B', 'C', 's', 's', 'as', 'ad', 'sas', 'as', 'sa', 'asd', 'asd', 'asd', 'asdf', 'asdf'],
['H', 'B', 'C', 's', 's', 'as', 'ad', 'sas', 'as', 'sa', 'asd', 'asd', 'asd', 'asdf', 'asdf'],
['J', 'K', 'L', 'B', 'C', 's', 's', 'as', 'ad', 'sas', 'as', 'sa', 'asd', 'asd', 'asd', 'asdf', 'asdf'],
];
function concatAll(items){
var base = items[0]

return mulit(base, items.slice(1))

function mulit(base, leftArr){
var multiplier = leftArr[0];

var newBase = [];
for(var i = 0, len = base.length; i < len; i++){
var b = base[i];
for(var j = 0, len2 = multiplier.length; j < len2; j++){
var m = multiplier[j];
newBase.push(b+''+m);
}
}

var _left = leftArr.slice(1);
if(_left.length){
return mulit(newBase, _left);
}else{
return newBase;
}
}
}

function concatAll2(arr) {
var funcBody = 'var ret = [];',
IDX = ['i', 'j', 'k', 'l'];

for (var i = 0, len = arr.length; i < len; i++) {
var idx = IDX[i];

funcBody += 'for (var ' + idx + ' = 0;' + idx + '< arr[' + i + '].length;' + idx + '++){';
if (i == len - 1) {
var mash = [];
for (var j = 0; j < arr.length; j++) {
var idx2 = IDX[j]
mash.push('arr[' + j + '][' + idx2 + ']');
}
funcBody += 'ret.push(' + mash.join('+') + ')';
}
}

for (var i = 0, len = arr.length; i < len; i++) {
funcBody += '}';
}

funcBody += 'return ret;'

return (new Function('arr', funcBody)(arr));
}

//输出一维数组
function concatAll3(items) {
var res = [
[]
];
for (var i = 0; i < items.length; i++) {
}
var arr = [];
for (var i = 0; i < item.length; i++) {
for (var j = 0; j < parent.length; j++) {
//arr.push(parent[j].concat([item[i]]));
arr.push(parent[j]+""+item[i]);
}
}
return arr;
}

return res;
}

function concatAll_tiejun(arr) {
var A,B,a,b, i, j,
ret = arr.shift();

while(B = arr.shift()){
A = ret.concat();
ret = [];
for (i=0; a = A[i++];) {
for (j = 0; b = B[j++];) {
ret.push(a + "" + b)
}
}
}

return ret;
}

function chongqi(data){//by chongqi
var item ,idx= -1,
total=1,
//±ê¼Ç×îºóÒ»¸öµÄÊý×é
length = data.length,
lenarr = [],
nowidx = [],

retval = [];
while ( item = data[++idx] ) {
total *= item.length;
lenarr[idx] = item.length;
nowidx[idx] = 0;
}

while ( total-- ) {
var rv = '',arr,
last,i  = 0;

while( i< length ){
rv += data[i++][0];
}
retval.push(rv);

i = nowidx.length;
while( i-- ){
arr = data[ i ];

//arr.push(arr.shift());
last= arr[idx=arr.length-1];
arr[idx] = arr[0];
arr[0] = last;
if (++nowidx[i]<lenarr[i]){
break;
} else {
nowidx[i] =0;
}
}

}
return retval;
}
//写死的4层循环
function func4(arr) {
var ret = [];for (var i = 0;i< arr[0].length;i++){for (var j = 0;j< arr[1].length;j++){for (var k = 0;k< arr[2].length;k++){for (var l = 0;l< arr[3].length;l++){ret.push(arr[0][i]+arr[1][j]+arr[2][k]+arr[3][l])}}}}return ret;
}
};

</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
é¥­é±¼ï¼šé€’å½’
``concatAll(saleProp);``
pending…
é¥­é±¼ï¼šåŠ¨æ€åˆ›å»ºå‡½æ•°
``concatAll2(saleProp);``
pending…
æ–‡ç‘œ-å¹¶è¡Œå¾ªçŽ¯-è¾“å‡º1ç»´æ•°ç»„
``concatAll3(saleProp);``
pending…
é“å†›3å±‚for å¾ªçŽ¯
``concatAll_tiejun(saleProp);``
pending…
é‡å¯ç‰ˆæœ¬
``chongqi(saleProp);``
pending…
å†™æ­»çš„å››å±‚å¾ªçŽ¯
``func4(saleProp);``
pending…

## Revisions

You can edit these tests or add even more tests to this page by appending `/edit` to the URL.