Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS chunk huge array
(version: 0)
Comparing performance of:
1 vs 2 vs 3 vs 4 vs 5 vs 6 vs 7 vs 8 vs 9 vs 10
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function arr() { return Array.from({ length: 100000 }, (_, idx) => `${++idx}`); } function chunk1(xs, n) { return xs.reduce(function (o, x, i) { const chunkIndex = Math.floor(i / n); if (!o[chunkIndex]) { o[chunkIndex] = []; } o[chunkIndex].push(x); return o; }, []); } 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(arr(), 1000);
2
chunk2(arr(), 1000);
3
chunk3(arr(), 1000);
4
chunk4(arr(), 1000);
5
chunk5(arr(), 1000);
6
chunk6(arr(), 1000);
7
chunk7(arr(), 1000);
8
chunk8(arr(), 1000);
9
chunk9(arr(), 1000);
10
chunk10(arr(), 1000);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (10)
Previous results
Fork
Test case name
Result
1
2
3
4
5
6
7
8
9
10
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):
I'm ready to help! Please go ahead and provide the benchmark results in a format that's easy for me to parse, such as JSON or a plain text list. I'll be happy to assist you with analyzing and interpreting the results. What would you like to know about the benchmark? The average execution time, the fastest test, or something else?
Related benchmarks:
JS array chunk
JS chunk array
JS chunk arrays 2
Lodash Chunk vs Native Reduce v2
Comments
Confirm delete:
Do you really want to delete benchmark?