When using the .reduce method for arrays or objects, there are serious performance problems due to the internal operating mechanism of the method.
This problem occurs when working with an array of thousands or more elements, depending on the computer power. Each time the method creates a new object and transfers all the fields of the old one there, adding a new field. This transfer takes a huge amount of resources and time.
Code example:
Solution 1. We will not recreate acc at each iteration, but add one new object property instead:
Code example
Solution 2. Use forEach() method:
Code example
Take advantage of these methods and optimize your applications! Learn more how else we use JavaScript and its frameworks on Twelvedevs website