# AS3 loops, performance comparison

Recently I did some benchmarks to determine the fastest loop in AS3. Here are the result:

 performance decrease in percentempty loop / variable assigment in body loop code AS3 AS2 `for (i = 0; i < k; i++) {}` 0*/7 0/0 `for (i = 0; i++ < k;) {}` 35/0 0/3 `i = 0; while (i < k) { i++; }` 5/6 0/2 `i = 0; while (i++ < k) {}` 35/3 0/3 `i = k; while (i-- > 0) {}` 5/0 5/1 `i = k; while (i--) {}` 20/0 5/5 for (each in o) compared to fastest loop 2800 30 for each (i in o) compared to fastest loop 45 N/A * fastest loop taken as reference value

So the main thing to keep in mind is that a for..in loop is now roughly 28 times slower than a standard integer loop - in AS2 the impact wasn't that big. Also don't overload the loop header, but put everything in the loop body instead, for example:

```// slower
var k:int = 1000;
for (var j:int = k - 1, i:int = 0; i < k; j = i, i++)
{
}

// faster
var k:int = 1000;
var j:int = k - 1;
for (var i:int = 0; i < k; i++)
{
j = i;
}
```

Another importing thing is integer casting when accessing array elements:
The second example is roughly 20% faster than the first one:

```// i*2 gets promoted to Number
for (var i:int = 0; i < 10000; i++)
{
a[i*2] = 0;
}

// Goes through fast path
for (var i:int =0; i < 10000; i++)
{
a[int(i*2)] = 1;
}
```

This was pointed out on slide 26 ("Promotion of Numeric Types") in the paper
ActionScript 3.0 and AVM2: Performance Tuning by Gary Grossman.

#### Conclusion

So, in AS3 there isn't really that much of a difference between the loop variations. The benchmark results were made by iterating an empty loop with 10.000.000 iterations. From this point of view, it doesn't matter at all.

If you have many loops which only get or set values and do simple arithmetics, you will perhaps notice a difference. But you have to always look at the broader image and find the bottleneck of the application, and a loop is the last thing you would optimize at first place.

## 14 thoughts on “AS3 loops, performance comparison”

1. it seems to me there is one big mistake in your actionscript code:
flash opimise empty loops by bypassing them … so your benchmark result must by faulsed by this

2. hey tooy, I don’t know how smart the compiler is, but I have updated the table which compares an empty loop vs a non-empty loop.

If flash skips empty loops during compilation or runtime my benchmark result would all have been 0ms. But I do get some result. For example the first AS3 loop takes about ~30ms, the loop with variable assigment is a little bit slower.

3. yeap that’s not really a mistake or a litle one ;)
0ms or not 0ms that’s the question.. but that’s a fact : flash compiler is a strange monster :)

another litle thing:
–i is faster than i– as ++i is faster than i++ (lite difference)

but as you say they’r many others things to optimize before loops

4. Oz says:

Nice, you have some good optimizations points in there. I’ll be trying integer casting when accessing arrays in the FisixEngine… 20% faster eh?

5. Oz says:

i just reread the part about integer casting. Now, is that only faster when you manipulate i? (in the case that i was originally an int)

6. Paul K says:

That’s what I understand, Oz. The i*2 returns a Number so you have to cast it back to an int. But if the code was originally just a[i]=0 then you’d be good.

7. I think if you use[i + something] this could result in a number or a string (associative array), so by declaring (i + something) as an int flash knows in advance how to access the array element.

8. Steven says:

I believe the fastest loop in Flash is actually

var i = k;
while (–i -(-1)) {}

Predecrementation is less bytecode than postdecrementation and subtraction is less than addition. So, –i is faster than i– and minus negative one is faster than plus one.