<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
window.obj = {};
for (var i = 0, len = 1001; i < len; i++) {
var arr = []
obj['key' + i] = arr
for (var n = 0, len = 1001; n < len; n++) {
arr.push({ ['key' + n]: n });
}
}
_.reduce(obj, (acc, val, key) => {
acc[key] = val.map(d => ({ label: key, value: val }));
return acc;
}, {})
Object.entries(obj).reduce((acc, [key, val]) => {
acc[key] = val.map(d => ({ label: key, value: val }));
return acc;
}, {})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Native |
Test name | Executions per second |
---|---|
Lodash | 30.1 Ops/sec |
Native | 24.9 Ops/sec |
Let's break down the provided benchmark test cases.
What is tested:
The provided benchmark tests two approaches to transforming an object with nested arrays into an object with key-value pairs, where each value is an array of objects containing a label and a value.
Options compared:
Two options are compared:
reduce
method from Lodash, a popular JavaScript utility library.Object.entries
, Array.prototype.map
) to achieve the same transformation.Pros and Cons of each approach:
Library used:
reduce
.Special JS feature or syntax:
There are no special features or syntax mentioned in the benchmark definition. However, it's worth noting that the use of arrow functions (=>
) and template literals (${}
) is common in modern JavaScript code.
Other alternatives:
If you're interested in exploring alternative approaches to this problem, here are a few options:
Keep in mind that the performance and readability implications of these alternative approaches will vary depending on your specific use case and requirements.