Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Many of us probably already know this:

var list = ...
var index = list.length

while( index-- ) {
    // do something
}

It's supposedly the fastest way to do a loop in javascript, since you avoid an extra test. So far, over the past years, I used this technique when dealing with with data where speed was important and the order didn't really matter.

But now I stumbled uppon an article that says this is actually slower, when dealing with arrays.

Which makes you avoid an extra test (compared to the standard for loop). But you know what ? this will be much slower than using the right order. Because all CPU caches in the world expect the processing to be ‘straight’, you will have cache misses again and again, and a 2X slow down is what you’ll get when you are lucky.

So do not loop backward unless you have very good reasons to do so.

Source: https://gamealchemist.wordpress.com/2013/05/01/lets-get-those-javascript-arrays-to-work-fast/

Now I'm curious! I only have limited possibilities to test theese things, and every other place I found still says that a backwards loop is the fastest possible way (even multiple answers on stackoverflow). Is that really true when dealing with (possibly large) arrays?

And before the premature optimization answer pops up (like it often does with this type of question): This is mainly just curiosity and yes, in things like games, performance matters!

About jsperf: So far, jsperf seems to imply that the backwards loop is faster (I can't check the tests right now, since it doesn't load the result on any atm - so I'm recalling what I saw before). That's the source of this question: The two informations are contradicting themselves - at least if that what's stated in that article is true! So what's "correct", in the end?

share|improve this question
2  
Why don't you just test it in jsperf in the browsers you care about (takes a few minutes to get your first results)? ALL performance questions MUST be answered with testing in the environment you care about. Posting this question without any testing of your own seems to indicate that you just want someone else to do the testing for you. – jfriend00 Aug 8 at 8:10
    
well fact related the cache miss is true. CPU expects array to be traversed in order. – Sushant Aug 8 at 8:12
    
Also, since JS engines are regularly adding performance enhancements, it is entirely possible that some or many browsers have added optimizations for the typical array iteration for loop to speed it up. Something you read a few years ago, may not still hold true today. – jfriend00 Aug 8 at 8:12
    
@jfriend00 Yes, but that's also the problem (and reason for this question): jsperf seems to imply that the backwards loop is faster (I've seen a couple of tests floating around here during the years). So jsperf actually contradicts this - but maybe that's also the case because those tests are too focused on one thing? I was really interested to hear if the argument "Because all CPU caches in the world expect the processing to be 'straight', you will have cache misses..." is grounded in reality - because if that's the case, backwards loops should theoretically be slower, right? – Katai Aug 8 at 8:15
1  
If you have current jsperf performance data in several browsers, then post it. The question really is meaningless without some performance data. There's zero useful discussion to be had until measurements are taken to know what the current state of affairs is. You don't figure out which thing is going to be faster by theorizing about things. You measure. You may try to explain a measurement by theorizing, but NOT the other way around. – jfriend00 Aug 8 at 8:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.