Test name | Executions per second |
---|---|
1 | 935711.9 Ops/sec |
2 | 4401085.0 Ops/sec |
3 | 5736018.5 Ops/sec |
4 | 7275306.0 Ops/sec |
5 | 4125797.5 Ops/sec |
6 | 3989397.8 Ops/sec |
7 | 2961834.8 Ops/sec |
8 | 7294280.0 Ops/sec |
9 | 1618355.2 Ops/sec |
10 | 8049551.0 Ops/sec |
function chunk1(xs, n) {
return xs.reduce(function(o, x, i) {
o[Math.floor(i / n)].push(x);
return o;
}, Array(Math.ceil(xs.length / n)).fill([]));
}
function chunk2(xs, n) {
return (xs.length <= n) ? [xs] : [
xs.slice(0, n),
chunk2(xs.slice(n), n)
]
}
function chunk3(array, size) {
//declaring variable 'chunked' as an empty array
let chunked = []
//looping through the array until it has been entirely "manipulated" or split into our subarrays
while (array.length > 0) {
//taking the spliced segments completely out of our original array
//pushing these subarrays into our new "chunked" array
chunked.push(array.splice(0, size))
}
//returning the new array of subarrays
return chunked
}
function chunk4(array, size) {
//declaring variable 'chunked' as an empty array
let chunked = []
//setting our start point for our while loop at index 0
let i = 0;
//looping through the array until we have reached the final index
while (i < array.length) {
//push the sliced subarray of length 'size' into our new 'chunked' array
chunked.push(array.slice(i, i + size))
//increment by the size as to avoid duplicates
i += size;
}
//return the array of subarrays
return chunked
}
function chunk5(array, size) {
//declaring variable 'chunked' as an empty array
let chunked = [];
//for loop iterating through every element of our input array
for (let ele of array) {
//declaring variable 'last' as the last index of our 'chunked' array
const last = chunked[chunked.length - 1];
//checking if last is undefined or if the last subarray is equal to the size
if (!last || last.length === size) {
//then we push the element to be a new subarray in 'chunked'
chunked.push([ele])
} else {
//if not, then we add the element to the 'last' subarray
last.push(ele)
}
}
//return the array of subarrays
return chunked
}
function chunk6(array, size) {
const chunked_arr = [];
for (let i = 0; i < array.length; i++) {
const last = chunked_arr[chunked_arr.length - 1];
if (!last || last.length === size) {
chunked_arr.push([array[i]]);
} else {
last.push(array[i]);
}
}
return chunked_arr;
}
function chunk7(array, size) {
const chunked_arr = [];
let copied = [array]; // ES6 destructuring
const numOfChild = Math.ceil(copied.length / size); // Round up to the nearest integer
for (let i = 0; i < numOfChild; i++) {
chunked_arr.push(copied.splice(0, size));
}
return chunked_arr;
}
function chunk8(array, size) {
const chunked_arr = [];
let index = 0;
while (index < array.length) {
chunked_arr.push(array.slice(index, size + index));
index += size;
}
return chunked_arr;
}
function chunk9(array, size) {
if (!array) return [];
const firstChunk = array.slice(0, size); // create the first chunk of the given array
if (!firstChunk.length) {
return array; // this is the base case to terminal the recursive
}
return [firstChunk].concat(chunk9(array.slice(size, array.length), size));
}
function chunk10(arr, n) {
let chunked = arr.reduce((chunk, val) => {
if (chunk[chunk.length - 1].length === n)
chunk.push([]);
chunk[chunk.length - 1].push(val);
return chunk;
}, [
[]
]);
return chunked
}
chunk1([1, 2, 3, 4], 2);
chunk2([1, 2, 3, 4], 2);
chunk3([1, 2, 3, 4], 2);
chunk4([1, 2, 3, 4], 2);
chunk5([1, 2, 3, 4], 2);
chunk6([1, 2, 3, 4], 2);
chunk7([1, 2, 3, 4], 2);
chunk8([1, 2, 3, 4], 2);
chunk9([1, 2, 3, 4], 2);
chunk10([1, 2, 3, 4], 2);