Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
JS chunk array
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser:
Chrome 120
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
1
2028422.9 Ops/sec
2
9403408.0 Ops/sec
3
10837425.0 Ops/sec
4
13245288.0 Ops/sec
5
9215315.0 Ops/sec
6
8977838.0 Ops/sec
7
3746918.0 Ops/sec
8
13233066.0 Ops/sec
9
3556473.5 Ops/sec
10
13831012.0 Ops/sec
Script Preparation code:
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 }
Tests:
1
chunk1([1, 2, 3, 4], 2);
2
chunk2([1, 2, 3, 4], 2);
3
chunk3([1, 2, 3, 4], 2);
4
chunk4([1, 2, 3, 4], 2);
5
chunk5([1, 2, 3, 4], 2);
6
chunk6([1, 2, 3, 4], 2);
7
chunk7([1, 2, 3, 4], 2);
8
chunk8([1, 2, 3, 4], 2);
9
chunk9([1, 2, 3, 4], 2);
10
chunk10([1, 2, 3, 4], 2);