Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array chunk
(version: 0)
compare between array chunk alternatives
Comparing performance of:
chunk array with while vs Chunk Array with es6
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
values = [1470002400000, 1470088800000, 1470175200000, 1470261600000, 1470348000000, 1470434400000, 1470520800000, 1470607200000, 1470693600000, 1470780000000, 1470866400000, 1470952800000, 1471039200000, 1471125600000, 1471212000000, 1471298400000, 1471384800000, 1471471200000, 1471557600000, 1471644000000, 1471730400000, 1471816800000, 1471903200000, 1471989600000, 1472076000000, 1472162400000, 1472248800000, 1472335200000, 1472421600000, 1472508000000, 1472594400000, 1472680800000, 1472767200000, 1472853600000, 1472940000000, 1473026400000, 1473112800000, 1473199200000, 1473285600000, 1473372000000, 1473458400000, 1473544800000, 1473631200000, 1473717600000, 1473804000000, 1473890400000, 1473976800000, 1474063200000, 1474149600000, 1474236000000, 1474322400000, 1474408800000, 1474495200000, 1474581600000, 1474668000000, 1474754400000, 1474840800000, 1474927200000, 1475013600000, 1475100000000, 1475186400000, 1475272800000, 1475359200000, 1475445600000, 1475532000000, 1475618400000, 1475704800000, 1475791200000, 1475877600000, 1475964000000, 1476050400000, 1476136800000, 1476223200000, 1476309600000, 1476396000000, 1476482400000, 1476568800000, 1476655200000, 1476741600000, 1476828000000, 1476914400000, 1477000800000, 1477087200000, 1477173600000, 1477260000000, 1477346400000, 1477432800000, 1477519200000, 1477605600000, 1477692000000, 1477778400000, 1477868400000, 1477954800000, 1478041200000, 1478127600000, 1478214000000, 1478300400000, 1478386800000, 1478473200000, 1478559600000]
Tests:
chunk array with while
function chunkArray(myArray, chunk_size) { const results = []; while (myArray.length) { results.push(myArray.splice(0, chunk_size)); } return results; } result = chunkArray(values, 12);
Chunk Array with es6
function chunkArray(arr, size) { return Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size), ); } result = chunkArray(values, 12);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
chunk array with while
Chunk Array with es6
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 benchmark and its test cases. **Benchmark Overview** The benchmark is designed to compare two approaches for chunking arrays: using a `while` loop and using `Array.from()` with a closure. The benchmark creates an array of timestamps, splits it into chunks of 12 elements each, and measures the execution time for both approaches. **Test Cases** There are two test cases: 1. **"chunk array with while"`**: This test case uses a traditional `while` loop to split the array into chunks. The code is straightforward and easy to understand. 2. **"Chunk Array with es6"`**: This test case uses the newer `Array.from()` method with a closure to create an array of chunked elements. This approach is often preferred for its conciseness and readability. **Pros and Cons** * **"chunk array with while"`**: + Pros: easy to understand, well-established syntax. + Cons: might be slower due to the overhead of the `while` loop. * **"Chunk Array with es6"`**: + Pros: concise, efficient, and expressive syntax. + Cons: might be less intuitive for developers without experience with modern JavaScript features. **Library/Functionality** None of the test cases use any external libraries or functions beyond the standard JavaScript `Array` methods. The `Array.from()` method is a built-in feature introduced in ECMAScript 2015 (ES6). **Special JS Feature/Syntax** The only special syntax used here is the arrow function in the "Chunk Array with es6" test case, which was introduced in ECMAScript 2015. This allows for concise and expressive code without using traditional `function` declarations. **Alternatives** If you were to implement this benchmark from scratch, you could use other approaches to chunk the array, such as: * Using a custom loop or recursive function. * Utilizing libraries like Lodash or Underscore.js, which provide utility functions for array manipulation. * Exploring alternative data structures, like buffers or streams, for more efficient chunking. However, given the simplicity and expressiveness of `Array.from()` with a closure, it's likely that this approach would be the preferred choice for implementing such a benchmark.
Related benchmarks:
lodash vs un
Lodash.isEqual vs Array.join() Equality Comparison for Shallow Array of integers.
Lodash.isEqual vs ES6 Sets Equality Comparison for Shallow Array of numbers.
set.has vs. array.includes vs array.indexOf (string values) - 800 ids
Comments
Confirm delete:
Do you really want to delete benchmark?