Test name | Executions per second |
---|---|
chunks_yield | 1004751744.0 Ops/sec |
chunks_perf | 1009620288.0 Ops/sec |
function chunks_yield(array, size) {
function* chunk(arr, n) {
for (let i = 0; i < arr.length; i += n) {
yield arr.slice(i, i + n);
}
}
return [chunk(array, size)];
}
function chunks(arr, n) {
return arr.reduce((chunk, val) => {
if (chunk[chunk.length - 1].length === n)
chunk.push([]);
chunk[chunk.length - 1].push(val);
return chunk;
}, [
[]
]);
}