consider using a backwards while-loop for map
See original GitHub issuemap
and other methods could be made a tiny bit faster when we would use a backwards loop, see http://stackoverflow.com/a/23998651/1246198 why it’s faster and so on.
...
**Look at this**
for( var index = 0 , length = array.length ; index < length ; index++ ) {
//do stuff
}
1. Need to create at least 2 variables (index,length)
2. Need to check if the index is smaller than the length
3. Need to increase the index
4. the `for` loop has 3 parameters
**Now tell me why this should be faster than:**
var length = array.length;
while( --length ) { //or length--
//do stuff
}
1. One variable
2. No checks
3. the index is decreased (Machines prefer that)
4. `while` has only one parameter
...
This is just a suggestion, doesn’t have to be this way in any way 😉
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
How to iterate over a Set or Map in reverse order ...
There is no way to obtain a reversed iterator on Maps or Sets, as I have found out while trying to get the...
Read more >How to iterate a map in reverse order – C++
In this article we will discuss how to iterate a map in reverse order. Map store the elements in sorted order of keys....
Read more >Map() vs for() in Javscript for looping reversely
I want to loop over this array in a reverse manner and return a new array. So, my first guess was to use...
Read more >How to traverse a STL map in reverse direction?
Now for traversing in reverse order we will iterate over the range b/w rbegin() & rend() using reverse_iterator. Reverse Iteration in map:
Read more >Reverse the order of map in Apex or VF
You can't loop over maps directly, so you're either looping over the map's keySet (which is a Set<Object> , and similarly unordered), or...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Benchmark runs these as many times as it can. (It’s what underlies JSPerf, and a successor of sorts to JSLitmus.)
And I’m still not convinced:
Mostly I’m not convinced of the worth of chasing such micro-optimizations. If there is a really substantive difference, I would certainly be interested in talking about it. But I don’t see that here. I see years of history of engines one-upping each other in various parts, and performance gaining unevenly because of that, but no particular algorithmic advantage.
Now imagine a larger array and the function being run many times in just a few seconds (quite the real world scenario, with React + some render logic)