var arr = [
{
"opportunityId": 5943111,
"findingList": [
{
"findingId": 6919
}
],
},
{
"opportunityId": 5943111,
"findingList": [
{
"findingId": 6918,
"findingId": 6917
}
]
}
]
arr.reduce((acc, findingInOpp) => [acc, findingInOpp.findingList.map(({ findingId }) => findingId)],[])
var arr = [
{
"opportunityId": 5943111,
"findingList": [
{
"findingId": 6919
}
],
},
{
"opportunityId": 5943111,
"findingList": [
{
"findingId": 6918,
"findingId": 6917
}
]
}
]
arr.reduce((acc, findingInOpp) => {
acc.push(findingInOpp.findingList.map(({ findingId }) => findingId))
return acc
},[])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread operator | |
Push |
Test name | Executions per second |
---|---|
spread operator | 6797571.5 Ops/sec |
Push | 6362141.0 Ops/sec |
Let's break down the provided JSON data and explain what's being tested.
Benchmark Definition
The benchmark is comparing two approaches:
...
).push()
method inside the reduce()
function.Description of Options Compared
map()
function, which returns another array with the desired values.push()
to add elements to the accumulator array inside the reduce()
function.Pros and Cons of Each Approach
push()
on arrays, as it avoids the need to create a new array.reduce()
function.Library Used
There is no specific library mentioned in the benchmark definition. However, it's likely that the reduce()
function and the map()
function are part of the built-in JavaScript API.
Special JS Feature or Syntax
The use of the spread operator (...
) is a feature introduced in ECMAScript 2015 (ES6). It allows for concise array creation by spreading elements from one array into another.
Other Considerations
Alternatives
If you're interested in exploring alternative approaches or optimizations, consider:
forEach()
or filter()
, to reduce the size of the accumulator array.Keep in mind that the benchmark result should be taken as a starting point, and further testing may be necessary to confirm the findings and identify potential biases.