Loop optimization
Here’s a little nuance about loops that I never thought of before. If you are looping through an array you would probably write the code like this:
output:0 of 4 : Star Wars
1 of 5 : LOTR
2 of 5 : Matrix
3 of 5 : Superman
4 of 5 : Close Encounters
You’ll notice that I’m adding an element to the array which I’m looping through. That’s almost never a good idea. However I want to illustrate something with this. I add to the array after tracing the output so the first time through the loop the length is 4. Each time afterwards it was 5. And the for/loop noticed this.
Here’s the important thing: each time through the loop the for-conditions are rechecked. That means movies.length is evaluated over and over again. To optimize the loop we could create a variable that holds the value of the array length and then use that in the for-condition. That way the array length is only evaluated once.
The change would look like this:
output:
0 of 4 : Star Wars
1 of 5 : LOTR
2 of 5 : Matrix
3 of 5 : Superman
You can see that the movies.length was still increased but the loop didn’t run through it because the variable is set before loop runs.
If the array that you are looping through is going to change during the loop then you must use the first approach. But it’s unlikely that you’ll do that often, or hopefully ever, so go with the second case and you’re code will run faster.
While you’re at it you can optimize even further by looping through the array backwards. The Flash VM can count backwards through a loop faster than it can forwards.













Another reason to loop backwards is when you DO need to change the array within the loop. For example, say you needed to remove all elements of an array whose value was “killme”. You’d need to loop through the array, but you’d also need to change the array. When you remove items from the array, your indexing gets messed up if you are moving forwards. but backwards is like:
for(var i:Number = items.length-1; i >= 0; i–)
{
if(items[i] == “killme”) items.splice(i, 1);
}
If I wrote that correctly (i’m rushing…) it should remove the elements correctly, and not mess up the indexing in the process.
Lately, I’ve been trying to remember to grab the array length before looping. I don’t know if it helps much, but for some reason I feel better doing it.
I would also like to add a thank you to Keith for his example. That’s a great trick, and I’ll do my best to remember to use it in the future. Thanks, man!
Good example.
You know, you and I are the kind of guys who would sit around a camp fire and talk about “when it’s a good idea to loop backwards through an array.” :-)
My wife is rolling here eyes as she reads this.
Of all the code-optimization tips, storing an array’s length in a variable is the biggest performance boost. You can apply the rule of threes here: if you expect to access a calculated value (like a clip’s _x or an array’s length) 3 or more times, store it in an array. Perhaps the inverse is easier to understand: if you only plan to access a variable’s value 1 or 2 times, no sense storing it in a variable in the first place.
Great tip. This is especially effective when checking the length of long strings.
You can see some other performance tips with times on this great site:
http://www.oddhammer.com/actionscriptperformance/set3/
Cheers,
Dustin Senos
Phillip, “rule of three” sounds like something for Lord of the Rings but I like it. :-)
I think that in many instances it can also aid in code readability to create local variables.
Dustin, thanks for the link. Good stuff.
Welcome back (Change)
Leave a Reply
+RIP : Spam Comments [ 2003?- November 20, 2008 ]
+Escaping apostrophies in AIR / SQLite
+Using the ObjectUtil class to introspect Objects in 4-dimensional space