Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
testachunk2
(version: 0)
Comparing performance of:
1 vs 2 vs 3 vs 4 vs 5 vs 6
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
1
function chunkArraySlice(array, size) { let result = [] for (i = 0; i < array.length; i += size) { let chunk = array.slice(i, i + size) result.push(chunk) } return result } console.log(chunkArraySlice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 2));
2
const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size) ); console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 2));
3
function chunkArray(myArray, chunk_size){ var results = []; while (myArray.length) { results.push(myArray.splice(0, chunk_size)); } return results; } console.log(chunkArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 2));
4
function chunkArraySlice(array, size) { let result = [] let i = 0 array.forEach(() => { let chunk = array.slice(i, i + size) result.push(chunk) i+=size }) return result } console.log(chunkArraySlice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 2));
5
const chunkIt = (arr, size) => { let buckets = [] size = size != 0 ? Math.abs(size) : 1 // Just create the buckets/chunks storage for (let i = 1; i <= Math.ceil(arr.length / size); i++) { buckets.push([]) } // Put in the buckets/storage by index access only for (let i = 0; i < arr.length; i++) { var arrIndex = Math.ceil((i + 1) / size) - 1 buckets[arrIndex].push(arr[i]) } return buckets; } console.log(chunkIt([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 2));
6
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 } console.log(chunk10([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 2));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
1
2
3
4
5
6
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):
Let's break down the provided JSON and explain what is being tested. **Benchmark Definition** The benchmark definition represents a JavaScript function that takes an array as input and returns a new array with the same elements, but divided into smaller chunks of a specified size. The functions are: 1. `chunkArraySlice`: uses a traditional for loop to create chunks 2. `chunk`: uses `Array.from` and a closure to create chunks 3. `chunkArray`: uses `while` loop and `splice` method to create chunks 4. `chunkArraySlice` (again, but with a different implementation): uses `forEach` and `slice` methods to create chunks 5. `chunkIt`: uses a different approach to divide the array into chunks **Options Compared** The benchmark compares the performance of five different approaches to divide an array into smaller chunks: 1. Traditional for loop (`chunkArraySlice`) 2. Using `Array.from` and a closure (`chunk`) 3. Using a while loop and `splice` method (`chunkArray`) 4. Using `forEach` and `slice` methods (`chunkArraySlice` with different implementation) 5. Using a different approach to divide the array into chunks (`chunkIt`) **Pros and Cons** Here's a brief summary of each approach: 1. **Traditional for loop (chunkArraySlice)**: Simple and efficient, but may not be as flexible or scalable. 2. **Using Array.from and a closure (chunk)**: Flexible and scalable, but may have performance overhead due to the creation of an intermediate array. 3. **Using a while loop and splice method (chunkArray)**: Simple and efficient, but may not be as safe or predictable as other approaches (e.g., modifying the original array). 4. **Using forEach and slice methods (chunkArraySlice with different implementation)**: Similar to the traditional for loop approach, but with a different syntax. 5. **Using a different approach to divide the array into chunks (chunkIt)**: Unconventional and may not be as widely supported or understood. **Latest Benchmark Results** The latest benchmark results show that: 1. `chunkIt` has the highest execution rate (34440.203125 executions per second) 2. The other approaches are close in performance, with `chunk` and `chunkArraySlice` having slightly lower rates (33571.984375 and 32912.8203125 respectively) Overall, the benchmark results suggest that the approach using a while loop and `splice` method (`chunkArray`) is one of the most efficient ways to divide an array into smaller chunks, while `chunkIt` provides a unique solution that outperforms the others.
Related benchmarks:
Javascript if condition < 0
TestJoas012
testtesttesttesttesttest
fdsfdsf
try vs if // no error
Comments
Confirm delete:
Do you really want to delete benchmark?