jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
function flattenArray(arr) {
if (Array.isArray(arr)) {
return arr.reduce(function(done,curr){
return done.concat(flattenArray(curr));
}, []);
} else {
return arr;
}
}
var flattenReduceArray = (result, item) => Array.isArray(item) ? item.reduce(flattenReduceArray, result) : [...result, item]
function flattenReduceArrayES5 (result, item) {
if (Array.isArray(item)) {
return item.reduce(flattenReduceArrayES5, result)
} else {
result.push(item);
return result;
}
}
var flattenConcat = function(a, level) {
for (var i = 0, a = a.slice(); a.some(Array.isArray) && (i != level); i++)
a = Array.prototype.concat.apply([], a);
return a;
}
var arrayTest = [[[[2]]],[[1]],[3]];
Ready to run.
Test | Ops/sec | |
---|---|---|
flattenReduceArray |
| ready |
flattenArray |
| ready |
flattenReduceArrayES5 |
| ready |
flattenConcat |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.