DEV Community

Discussion on: Don’t use for loop for JavaScript Arrays

sharmakushal profile image
Kushal sharma Author

You can easily get the performace metic on online b/w for and other functions . And every thing has pro and cons . I also has used for loop insted of the map in many situations

Thread Thread
steveblue profile image
Steve Belovarich
Thread Thread
savagepixie profile image
SavagePixie • Edited on

Oh, you mean those tests in which if you place forEach before for then forEach becomes faster? The same tests in which if you actually use the result of the operation afterwards map becomes faster than for?

"Because performance" doesn't seem a particularly good argument to choose one option over another. How fast a for, map, etc will run depends on more than its name, like optimisations that the runtime can do. So just choosr whichever fits your needs and if you run into performance issues profile and see what's causing the bottleneck (which probably won't be that map that you chose to use instead of a for loop).

Thread Thread
steveblue profile image
Steve Belovarich

Yes! This is what I mean. Engineers should consider performance when deciding which method to use.

Some comments have been hidden by the post's author - find out more