Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
testachunk
(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 = [] // 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 benchmark and explain what is being tested. **Benchmark Overview** The benchmark tests six different implementations of chunking an array into smaller sub-arrays of a specified size. Chunking is a process where an array is divided into smaller chunks, often used in data processing and analysis. **Test Cases** Each test case represents a specific implementation of chunking: 1. **`chunkArraySlice` (array slicing)**: This implementation uses the `slice()` method to extract sub-arrays from the original array. 2. **`chunk` (using `Array.from`)**: This implementation uses the `Array.from()` constructor to create an array of chunks, and then maps over the original array to extract each chunk. 3. **`chunkArray` (using `splice`)**: This implementation uses the `splice()` method to remove elements from the original array in chunks. 4. **`chunkArraySlice (forEach loop)`**: This implementation uses a `forEach` loop to iterate over the original array and extract sub-arrays using the `slice()` method. 5. **`chunkIt` (bucket-based approach)**: This implementation creates an array of buckets, where each bucket is assigned a chunk from the original array. 6. **`chunk10` (using `reduce`)**: This implementation uses the `reduce()` method to accumulate chunks into an array. **Options Compared** The benchmark compares six different implementations of chunking: 1. Array slicing (`chunkArraySlice`) 2. Using `Array.from()` (`chunk`) 3. Using `splice()` (`chunkArray`) 4. Using a `forEach` loop (`chunkArraySlice (forEach loop)`) 5. Bucket-based approach (`chunkIt`) 6. Using `reduce()` (`chunk10`) **Browser and Platform** The benchmark is executed on Firefox 77, running on Linux x86-64 desktop hardware. **Performance** The benchmark reports the number of executions per second for each test case. The results show that: * `chunkArraySlice` (array slicing) has a slightly higher performance compared to other implementations. * `chunk10` (using `reduce`) has a lower performance compared to other implementations. Overall, the benchmark suggests that array slicing (`chunkArraySlice`) is one of the most efficient methods for chunking an array in JavaScript. However, the results may vary depending on the specific use case and implementation details.
Related benchmarks:
None test
Teste performance
delete vs undefined vs null - 4
TestJoas012
fdsfdsf
Comments
Confirm delete:
Do you really want to delete benchmark?