Skip to content

Create _benchmarks/combine-vs-es6-vs-permutate.md #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions _benchmarks/combine-vs-es6-vs-permutate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: combine vs es6 vs permutate
setup: |
var testArr = [
[void 0, 1, 2, 3],
[void 0, 'slovo', 'b', 'c'],
[void 0, 'x', 'y', 'z']
];
tests:
-
name: combine
code: |
var ret = [[]];
for (var i = 0; i < testArr.length; i++) {
var r = [];
var ret_length = ret.length;
for (var j = 0; j < testArr[i].length; j++) {
for (var k = 0; k < ret_length; k++) {
if(testArr[i][j]) {
r[j * ret_length + k] = ret[k].concat(testArr[i][j]);
} else {
r[j * ret_length + k] = ret[k];
}
}
}
ret = r;
}
return ret;
-
name: es6
code: |
const f = (a, b) => [].concat(...a.map(d => b.map(e => [].concat(d, e).filter(Boolean))));
const cartesian = (a, b, ...c) => (b ? cartesian(f(a, b), ...c) : a);
return cartesian(...testArr);
-
name: permutate
code: |
var known = {};
return testArr.reduce(function (sum, current) {
var arr = [].concat.apply([], Array(current.length).fill(sum));
var index = 0;
arr.forEach(function (item, i) {
arr[i] = current[index] ? ((item ? item : '') + current[index]) : false;
if (known[arr[i]]) {
arr[i] = false;
}
known[arr[i]] = true;
!((i + 1) % current.length) && index++;
index >= current.length && (index = 0);
});
return sum.concat(current).concat(arr);
}, []).filter(Boolean).map(function (i) {
return ('' + i).split('');
});
---