DEV Community

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

Collapse
 
steveblue profile image
Stephen Belovarich • Edited

Did you compare the performance of either in addition to considering the interface? Just because map can be more human readable doesn’t necessarily mean you shouldn’t use a for loop. for could be more performant for the end user.

Collapse
 
sharmakushal profile image
Kushal sharma • Edited

Yes, sir totally agreed with you that the for loop is much faster than the other javascript array functions. Everything has pros and cons. so we have to choose wisely what to use in which situation

Collapse
 
zicsus profile image
Himanshu Mishra

This is a very weak argument. I don't like the thinking that write whatever you wanna write because your pc's hardware can take it. Software is not in good place today because of that, especially web.
For and map have there own use cases. Use them wherever they suits the best.

Thread Thread
 
mburszley profile image
Maximilian Burszley • Edited

He's hiding any comments pointing out his weak arguments, so it makes sense. Terrible feature to be able to hide legitimate criticisms on DEV.

Thread Thread
 
zicsus profile image
Himanshu Mishra

Ohh! I didn't know you can that.
This is sad 😔
I wonder why dev would make this feature 🤔

Thread Thread
 
sharmakushal profile image
Kushal sharma

I made it hidden by mistake. I am new at this platform so just testing some features, if you know how I can revert it, then plz tell about the procedure

Collapse
 
steveblue profile image
Stephen Belovarich

Do you have actual data to back that up?

Thread Thread
 
sharmakushal profile image
Kushal sharma

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
Stephen Belovarich
Thread Thread
 
savagepixie profile image
SavagePixie • Edited

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
Stephen 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