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
var arr = [];
for (var i = 0; i< 1000; i++) {
arr.push(i);
}
function splitArrayReduce(items, subArrLength) {
return items.reduce((x, y, i) => {
if (i % subArrLength === 0) {
x.push([y]);
} else {
x.slice(-1)[0].push(y);
}
return x;
}, []);
};
function splitArrayWhile1(items, subArrLength) {
var arr = [];
var asdf = null;
do {
asdf = arr.length * subArrLength;
arr.push(items.slice(asdf, asdf + subArrLength));
} while (arr.slice(-1)[0].length === subArrLength);
return arr;
};
function splitArrayWhile2(items, subArrLength) {
var arr = [];
var asdf = null;
var end;
do {
asdf = arr.length * subArrLength;
end = asdf + subArrLength;
arr.push(items.slice(asdf, end));
} while (items.length > end);
return arr;
};
function doPaellaMadrilenya(items, subsections) {
if (subsections <= 0) return;
if (subsections >= items.length) return [items];
return items.reduce((a,b) => {
let group = a[a.length - 1];
if (!group || group.length >= subsections) {
a.push([b]);
} else {
group.push(b);
}
return a;
}, []);
}
function doPaellaRecursive(items, subArrLength, start = 0, end = start + subArrLength) {
const chorizo = items.slice(start, end);
return items.length > end
? [ chorizo, ...doPaellaRecursive(items, subArrLength, start + subArrLength) ]
: [ chorizo ];
}
function doPaellaValenciana(items, subsections) {
var paella = [];
var n = 0;
while (n < arr.length+subsections) {
if (n < arr.length) {
paella.push(arr.slice(n, n+subsections));
}
n += subsections;
}
return paella;
};
function splitArrayWhileManual(items, subArrLength) {
const desiredLength = items.length / subArrLength;
const arr = [];
let arrLength = arr.length;
while (arrLength < desiredLength) {
let start = arrLength * subArrLength;
let end = start + subArrLength;
const arr2 = arr[arrLength] = [];
while (start < end && items[start]) {
arr2[arr2.length] = items[start];
start++;
}
arrLength = arr.length;
};
return arr;
};
function splitArrayDoWhileManual(items, subArrLength) {
const itemsLength = items.length;
const arr = [];
let start = arr.length * subArrLength;
let end = start + subArrLength;
do {
start = arr.length * subArrLength;
end = start + subArrLength;
const arr2 = [];
while (start < end && items[start]) {
arr2[arr2.length] = items[start];
start++;
}
arr[arr.length] = arr2;
} while (itemsLength > end);
return arr;
};
function doPaellaValencianaWithRomero(items, subsections) {
var paella = [];
var n = 0;
var n2 = 0;
while (n < arr.length+subsections) {
if (n < arr.length) {
paella[n2] = arr.slice(n, n+subsections);
}
n += subsections;
n2 += 1;
}
return paella;
};
function splitArrayKvothe(array, num) {
let result = [];
for (let i = 0, l = array.length; i < l; i += num) {
result.push([array.slice(i, i + num)]);
}
return result;
};
function splitArraySplice(array, num) {
var arrCopy = array.slice(0);
let a = [];
while (arrCopy.length > 0) {
a.push(arrCopy.splice(0, num));
}
return a;
};
Ready to run.
Test | Ops/sec | |
---|---|---|
splitArrayReduce |
| ready |
splitArrayWhile1 |
| ready |
splitArrayWhile2 |
| ready |
doPaellaMadrilenya |
| ready |
doPaellaRecursive |
| ready |
doPaellaValenciana |
| ready |
doPaellaValencianaWithRomero |
| ready |
splitArrayWhileManual |
| ready |
splitArrayDoWhileManual |
| ready |
splitArrayKvothe |
| ready |
spliceArraySpliceDestructor |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.