
The following codes are for comparison between for loop and forEach() using an array of 20 consisting of millions items simple value addition and assignment where the execution times are printed.

Is that really true? To prove that using a for loop is more efficient than forEach(), I did the following experiment.Įxperiment: Comparison with Simple Value Addition and Assignment thisObject Object to use as this when executing callback. Syntax array.forEach (callback, thisObject) Parameter Details callback Function to test for each element. Removing optimizations that we had done on purpose forEach () method calls a function for each element in the array. I believe the above refactored codes also intend to achieve a better readability and maintainability, but the member of rxjs library rejects the changes and claim that it is

Those utility functions greatly improve our productivity in array operations by using functional programming paradigm and they are clearer to read and easier to understand. So basically, the author would like to refactor the for loop by using forEach() in array utility functions.Īs we know, JavaScript / TypeScript has a set of powerful array utility functions including forEach(), map(), reduce() and so on. Enter fullscreen mode Exit fullscreen mode
