Lets first Talk about the Array in the Programming world
I assume that if you belong from the programming world then you must playing with this stu...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
I was using map to handle loops, but I found out that it struggles when handling asynchronous requests. Map doesn't wait till the previous request end to start a new one, even when you use:
For this specific reason, I come back to for loops.
This is because you are returning an array of pending promises. If you want to fulfill these promises, you can use
Promise.all
. And if you want the result, you mustawait
the resolution ofPromise.all
. Or you can also use.then
.In considering this comment I discovered one can use .reduce() to return promises in sequence (which I also thought cool enough to share):
Good thoughts, that's for leading me to that find.
yes for loop is the fastest among all the other methods. yes everything has the pro and cons we have to choose wisely what to use in which situation
absolutely!
but that was a very helpful post!
I really like using map. It syntax combined with the fact that it doesn't change the original array it's very handy. If they improve async I'll definitely switch back to map (:
thanks, buddy, I am also the map syntax lover. I can't imagine the React without the map
Hopefully, JS will add something like IAsyncEnumrable like C#, sometime soon.
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.
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
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.
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.
Ohh! I didn't know you can that.
This is sad 😔
I wonder why dev would make this feature 🤔
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
Do you have actual data to back that up?
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
Like this...
github.com/dg92/Performance-Analys...
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 thatmap
that you chose to use instead of afor
loop).Yes! This is what I mean. Engineers should consider performance when deciding which method to use.
There are still perfectly valid use cases for the for loop. For example, if you want to break out of the loop early there’s the break keyword.
Or if you want to not iterate over the items of an array, but rather just repeat something a certain number of times, a for loop is useful there too.
A for loop also lets you have more complex logic for when you should continue looping. Remember that for loops are not only used for iterating over an array.
Aggred
Many comments about speed and performance here, but I believe in doing max-optimisations only when they make a noticeable difference for the end-user/service, thus I would endorse using Map if possible.
Yes shubham. While making the simple or small web app the map function doesn't make any impact on performance
Hey Kushal, Thanks for your post. I read your post and what made me more indulged into this topic was that you mentioned my home town "LUDHIANA" in your first example.
hehe, thanks buddy. I think why not to use the Punjab cities name, for example, let's also make them famous in dev.to
How are you going to use iterators then. Do use for loops, do use array functional methods too.
It depends upon what is the situation , if i had to just loop over then i will use foreach , else if i had to do asynchorons work inside loop i swith back to the for loop , as for each doesnot support asynchonous
What is map built on - is it a for loop underneath or is it a recursive function, or does that depend on the implementation of JS you are using? Not sure why it is slower than a for loop.
This post seems misguided from the very beginning.