<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var test = new Array(100000).fill(null).map(() => Math.random());
function chunk(data, size) {
const chunks = [];
const numChunks = Math.floor(data / size);
for (let i = 0; i <= numChunks; i++) {
const start = i * size;
chunks.push(data.slice(start, size));
}
return chunks;
}
chunk(test,5000)
_.chunk(test,5000)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Custom Chunk | |
lodash chunk |
Test name | Executions per second |
---|---|
Custom Chunk | 35.9 Ops/sec |
lodash chunk | 2858.3 Ops/sec |
Benchmark Explanation
The provided benchmark measures the performance difference between two approaches to chunking an array of random numbers.
Options Compared:
chunk
function manually calculates the number of chunks and creates them using Array.prototype.slice
._.chunk
function from the Lodash library, which is a popular utility library for JavaScript.Pros and Cons:
Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string formatting, and more. The _.chunk
function specifically chunking an array into equal-sized chunks.
In this benchmark, the _.chunk
function from Lodash is used to compare its performance with a custom implementation.
No Special JavaScript Features or Syntax
There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. The code only uses standard JavaScript concepts and libraries like Lodash.
Alternative Approaches:
Other approaches for chunking an array could include:
However, these alternatives are not included in the provided benchmark code and would require separate implementation and testing.