I have a dataset as such, where there may be multiple testResults
array elements, along with there being 1 or many response
array elements; and I an wanting to extract all of the potential completionDate
values into their own array.
const testResults = [
{
"value": {
"data": {
"response": [
{
"completionDate": "2024-10-10T17:44:00",
},
{
"completionDate": "2024-10-11T17:44:00",
}
]
}
}
},
...
];
I’m curious about the most effective/efficient way in which this could be done that is also readable by the rest of my team.
I have plenty of experience with using map
and flatMap
for things like this but have recently being using reduce
more and wonder if one is more advantageous over the other, and if there’s any other method I have not considered.
flatMap
const timestamps = testResults.flatMap(result =>
result.value.data.response.map(check => check?.completionDate)
);
reduce
const timestamps = testResults.reduce((acc, res) => {
res.value.data.response.forEach(check => {
acc.push(check.completionDate);
});
return acc;
}, []);
I did put use console.time
around each of them and found that
map: 0.103ms
reduce: 0.067ms
Does this really indicate that the reduce is that much more efficient?