<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var test = Array.from({
length: 100000
}, () => ({ key1: 'a', key2: 'b' }))
test.slice(0,25)
_.chunk(test,25)
test.slice(0,25)[1]
_.chunk(test,25)[1]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native slice | |
lodash chunk | |
Access values with native | |
Access values with lodash |
Test name | Executions per second |
---|---|
Native slice | 10058798.0 Ops/sec |
lodash chunk | 3278.8 Ops/sec |
Access values with native | 9626145.0 Ops/sec |
Access values with lodash | 3296.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Overview
The provided JSON represents a benchmark test case created on MeasureThat.net, a website that allows users to create and run JavaScript microbenchmarks. The goal is to compare the performance of different approaches when dealing with chunking a large list of objects.
Script Preparation Code
The script preparation code is:
var test = Array.from({ length: 100000 }, () => ({ key1: 'a', key2: 'b' }));
This creates an array test
containing 100,000 objects with two properties each. This large dataset will be used to benchmark different chunking approaches.
Html Preparation Code
The HTML preparation code includes a script tag that loads the Lodash library version 4.17.5:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, string manipulation, and more.
Benchmark Test Cases
There are four test cases:
test.slice(0,25)
This test case uses the native JavaScript slice()
method to extract a chunk of 25 elements from the test
array._chunk(test,25)
This test case uses the Lodash _chunk()
function to split the test
array into chunks of 25 elements.test.slice(0,25)[1]
This test case extracts a value from the first chunk of 25 elements using the native JavaScript syntax._chunk(test,25)[1]
This test case accesses the second element of the first chunk using the Lodash _chunk()
function.Library: Lodash
Lodash is a utility library that provides various functions for tasks like array manipulation, string manipulation, and more. In this benchmark, it's used to implement the _chunk()
function, which splits an array into chunks of a specified size.
Special JavaScript Features/Syntax
None mentioned in this benchmark.
Other Considerations
When dealing with large datasets and performance-critical code, microbenchmarks like this one can help identify optimization opportunities. However, it's essential to note that the results may not generalize to real-world scenarios or edge cases.
In addition to these test cases, you might also consider including other approaches, such as:
Alternatives
Some alternative approaches for chunking large datasets include:
Array.prototype.slice()
and manual iterationKeep in mind that each approach has its trade-offs in terms of performance, memory usage, and complexity. The choice of method depends on the specific requirements and constraints of your project.