Closed Rohitrky2021 closed 1 year ago
Use Array.prototype.reduce() instead of map() make the code more efficient:
Hey @Rohitrky2021, I don't understand how using reduce
here is more efficient than using map
. You might want to go through this stackoverflow question.
if modifying the original array is not a concern and the goal is to return a single value after iterating over the array, then using the reduce method as in the first function might be a more concise approach.the map method is more efficient in terms of performance, but the choice between the two ultimately depends on the specific needs of the implementation.
map
is better for this usecase, hence closing this pr.
if modifying the original array is not a concern
We're not changing the original array in any approach.
return a single value after iterating over the array
we want array of objects (one for each element in the original array)
However, if you're interested to contribute more to this project, please let me know. There are a couple of enhancements that I have in mind.
Use Array.prototype.reduce() instead of map() Using Array.prototype.reduce() instead of Array.prototype.map() can reduce the number of iterations over the array and make the code more efficient: