Many of us probably already know this:
var list = ... var index = list.length while( index-- ) {
This is supposedly the fastest way to make a loop in javascript, since you are avoiding the extra test. So far, in recent years, I have used this technique when working with data, where speed is important, and the order does not matter.
But now I stumbled upon an article which says that it is actually slower when working with arrays .
Which makes you avoid an extra test (compared to the standard for the cycle). But you know what? it will be much slower than using the correct order. Since all processor caches in the world expect that processing will be "direct", you will skip the cache again and again, and a 2x slowdown is what you get when you are lucky.
So do not go back unless you have good reason for this.
Source: https://gamealchemist.wordpress.com/2013/05/01/lets-get-those-javascript-arrays-to-work-fast/
Now I'm curious! I have only limited options for testing these things, and every other place I have found still says that the reverse loop is the fastest possible way (even a few stackoverflow answers). Is this really true when working with (possibly large) arrays?
And before the question of premature optimization comes up (as is often the case with questions of this type): this is basically just curiosity and yes, in things like games, performance matters!
About jsperf: So far, jsperf seemed to imply that the reverse loop is faster (I cannot check the tests now, since it does not download the result to any ATM - so I recall what I saw before). Here is the source of this question: two pieces of information contradict each other - at least if what is indicated in this article is true! So what is βrightβ in the end?