Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Chunks (2023.2)
(version: 2)
Comparing performance of:
chunks by reduceRight vs chunks by lodash vs splice from the beginning
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var arr = []; var size = 10 for (let i = 0; i < 1E6; i++) { arr.push(i); }
Tests:
chunks by reduceRight
const result = arr.reduceRight((res,_,__,self) => [...res, self.splice(0, size)],[]);
chunks by lodash
_.chunk(arr, 10);
splice from the beginning
const result = []; while (arr.length) { const chunk = arr.splice(0, size) result.push(chunk); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
chunks by reduceRight
chunks by lodash
splice from the beginning
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):
**Benchmark Overview** The provided benchmark measures the performance of three different approaches to chunking an array: `splice from the beginning`, `reduceRight`, and using the Lodash library (`_chunk`). **Chunking Approaches** 1. **Splice from the Beginning**: This approach uses a while loop to continuously remove elements from the beginning of the array in chunks, storing them in a new array. * Pros: Easy to implement, no external dependencies required. * Cons: Can be slow for large arrays due to the overhead of creating and managing the chunked arrays. 2. **ReduceRight**: This approach uses the `reduceRight` method on the array to create chunks. It iterates over the array from right to left, applying a callback function to each element that returns the current chunk. * Pros: Can be efficient for large arrays, as it avoids creating multiple intermediate arrays. * Cons: Requires JavaScript version support and may have performance issues due to the use of `reduceRight`. 3. **Lodash Chunking**: This approach uses the Lodash library's `_chunk` function to create chunks from the array. * Pros: Fast and efficient, as it leverages optimized C++ code under the hood. * Cons: Requires including an external library (Lodash) in the test. **Library Use** In this benchmark, the `lodash` library is used for the Lodash chunking approach. The `lodash.min.js` file is included via a script tag in the HTML preparation code. **Special JavaScript Feature or Syntax** The benchmark uses the `_` variable as a common scope reference in the `_chunk` function from Lodash, which is a convention in JavaScript to use `_` as a reserved name for variables that don't have an explicit `let` declaration. **Other Alternatives** If you wanted to implement chunking using other approaches, some alternatives could be: * Using `Array.prototype.slice()` and iterating over the array manually. * Utilizing modern array methods like `Array.prototype.reduce()` or `Array.prototype.map()`. * Employing native WebAssembly (WASM) performance optimizations, if available in your target browsers. Keep in mind that each approach has its trade-offs, and the best choice will depend on the specific requirements of your project.
Related benchmarks:
native-slice-vs-chunk
native slice vs lodash chunk
lodash-chunk-vs-custom-chunk
Chunk slice vs. Lodash chunk
Comments
Confirm delete:
Do you really want to delete benchmark?