var arr = [
{
"opportunityId": 5943111,
"findingList": [
{
"findingId": 6919
}
],
},
{
"opportunityId": 5943111,
"findingList": [
{
"findingId": 6918,
"findingId": 6917
}
]
}
]
arr.reduce((acc, findingInOpp) => [findingInOpp.findingList.map(({ findingId }) => findingId), acc],[])
var arr = [
{
"opportunityId": 5943111,
"findingList": [
{
"findingId": 6919
}
],
},
{
"opportunityId": 5943111,
"findingList": [
{
"findingId": 6918,
"findingId": 6917
}
]
}
]
arr.reduce((acc, findingInOpp) => {
acc.unshift(findingInOpp.findingList.map(({ findingId }) => findingId))
return acc
},[])
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
},[]).reverse()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread operator | |
unshift | |
push reverse |
Test name | Executions per second |
---|---|
spread operator | 9755999.0 Ops/sec |
unshift | 7921735.5 Ops/sec |
push reverse | 9931914.0 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
The benchmark is designed to compare the performance of three different approaches:
...
syntax to create a new array by spreading the elements of an existing array.unshift()
method.push()
method and then reverses the order of the elements.The benchmark is testing how these three approaches compare in terms of performance, specifically in reducing an array of objects while preserving the object's properties.
Pros and Cons:
reverse()
operation.Libraries and Special Features:
None of the benchmark cases use any external libraries. However, they do utilize some special JavaScript features:
...
syntax is used to create a new array by spreading elements from an existing array.Alternatives:
If you're interested in exploring alternative approaches, here are a few options:
concat()
instead of spread operator: This method creates a new array by concatenating two or more arrays. While it's not as concise as the spread operator, it can be useful if you need to combine multiple arrays.splice()
instead of push and reverse: This method modifies the original array by removing or inserting elements at a specified index. While it's not as intuitive as push and reverse, it can be useful for more complex data manipulation tasks.Keep in mind that these alternatives may have different performance characteristics and may require additional optimizations to achieve good results.