Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS chunk arrays 2
(version: 0)
Comparing performance of:
1 vs 2 vs 3 vs 4 vs 5 vs 6 vs 7 vs 8 vs 9 vs 10 vs 11
Created:
5 years ago
by:
Guest
Jump to the latest result
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 } function chunk11(array, size) { if (array.length <= size) { return [array] } return [array.slice(0, size), ...chunk11(array.slice(size), size)] }
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);
11
chunk11([1, 2, 3, 4], 2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (11)
Previous results
Fork
Test case name
Result
1
2
3
4
5
6
7
8
9
10
11
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
It looks like you provided a large JSON array of data, but I'll extract the relevant information from it. Here are the results: **Browser:** Firefox 86 **Device Platform:** Desktop **Operating System:** Mac OS X 10.15 **Executions Per Second:** 1. 8963598.0 (Test Name: 8) 2. 7043338.0 (Test Name: 10) 3. 7006373.5 (Test Name: 1) 4. 6547772.5 (Test Name: 2) 5. 6301928.0 (Test Name: 11) 6. 4369297.5 (Test Name: 6) 7. 4343218.5 (Test Name: 3) 8. 2993717.0 (Test Name: 5) 9. 2782838.75 (Test Name: 9) 10. 2514240.5 (Test Name: 7) **Test Names:** 1. TestName: "1" 2. TestName: "2" 3. TestName: "3" 4. TestName: "5" 5. TestName: "6" 6. TestName: "7" 7. TestName: "8" 8. TestName: "9" 9. TestName: "10" 10. TestName: "11" Please let me know what specific information you'd like me to extract from this data!
Related benchmarks:
JS array chunk
JS chunk array
Lodash Chunk vs Native Reduce v2
JS chunk huge array
Comments
Confirm delete:
Do you really want to delete benchmark?