Some ActionScript 3.0 Optimizations
Posted by Dennis on May 1, 2007 in ActionScript, Flash, Flex • 36 commentsI’ve been digging into ActionScript optimization and found some interesting results. Mind though that there’s nothing really new here, many of these tricks can be found in articles that I reference at the bottom of this posting.
Array indexing
Let’s start with a simple loop through the items of an array:
private function method1() : void
{
var tmpVar:int;
for(var i:Number=0; i<testArray.length; i++)
{
tmpVar = testArray[i];
}
}
Looping through this method 50 times gives an average time of 53.34 milliseconds. Now let’s change the type of iterator variable i from Number to int:
for(var i:int=0; i<testArray.length; i++)
This results in an average time of 35.58 milliseconds. This is because array indexing is faster with ints. We can also speed up the loop by storing the array’s length in a variable outside of the loop:
var l:int = testArray.length;
for(var i:int=0; i<l; i++)
Time taken: 21.6 milliseconds!
Constants from other classes
Another optimization that I found is in the way you use constants from another class. Take for instance this piece of code:
var tmpVar:int;
for(var i:Number=0; i<100000; i++)
{
tmpVar = SomeClass.SOME_CONSTANT;
}
This takes 34.08 milliseconds to execute. If we move the constant lookup outside of the iteration, like this:
var tmpVar:int;
var myConstant:int = SomeClass.SOME_CONSTANT;
for(var i:Number=0; i<100000; i++)
{
tmpVar = myConstant;
}
Now it only takes 15.8 milliseconds.
Variable instantiation
Variable instantation in this fashion:
for(var i:int=0; i<100000; i++)
{
var v1:Number=10;
var v2:Number=10;
var v3:Number=10;
var v4:Number=10;
var v5:Number=10;
}
takes 46.52 millis. This can be speeded up if you use the “var” keyword one time and then declare the variables on a single line, like this:
for(var i:int=0; i<100000; i++)
{
var v1:Number=10, v2:Number=10, v3:Number=10, v4:Number=10, v5:Number=10;
}
which results in an average time of 19.74 milliseconds.
Bitwise operators
Bitwise operators can also increase performance. Take for instance multiplication and division:
for(var i:int=0; i<100000; i++)
{
var val1:int = 4 * 2;
var val2:int = 4 * 4;
var val3:int = 4 / 2;
var val4:int = 4 / 4;
}
Average time: 49.12 ms, using bitwise operators it is taken down to 35.56 ms:
for(var i:int=0; i<100000; i++)
{
var val1:int = 4 << 1;
var val2:int = 4 << 2;
var val3:int = 4 >> 1;
var val4:int = 4 >> 2;
}
This is just a small piece of the pie though. There’s lots more to read about this subject:
- AS3 optimations & suggestions
- ActionScript 3.0 and AVM2: Performance Tuning
- Resource management strategies in Flash Player 9
- Understanding garbage collection in Flash Player 9
- gskinner.com: gBlog: Types in AS3: ints not so fast, uints slow!
- Actionscript optimization resources
- More performance tuning in Actionscript 3
- Bitwise gems - fast integer math
- AS3 and AVM Performance Tuning tips from Gary Grossman
- AS3: Rethink your old assumptions
- ActionScript 3 Performance Tuning by Matt Chotin
- Fast and accurate sine/cosine approximation
- Optimizations for AS3 calculations
- Fast Flash9 Fixed Point Sine
- AS3 Interesting Numeric Storage Behavior
- Flex RIA Performance Considerations Part 1: Getting Started
- Boost memory performance on data structures using HashMapCollection API
- Flash AS3 optimization - Fastest way to copy an array
- Making dispatchEvent More Efficient
- ActionScript 3 Vector / Array Performance Comparison
- ActionScript 3 Performance Tester
- Excellent article about benchmarking
- Top Ten ActionScript Performance Tips
- Tips on how to write efficient AS3 - part 1
- Tips on how to write efficient AS3 - part 2
- AS3 Performance Testing Harness
- Joa Ebert: Code Optimization
- LinkedGrid
- LinkedList
- Flex Databinding Performance




I remember Ted Patrick saying something about the overhead of using int over Number typing….hit him up about some of his knowledge.
Very helpful - thank you!
Hi there,
I found earlier following resources very usefull
bitwise
http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/
AS3 and AVM Performance Tuning tips from Gary Grossman
http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf
Useful links, thanks!
Dennis
Unlike c, the following:
var v1:Number=10, v2:Number=10, v3:Number=10, v4:Number=10, v5:Number=10;
is equal to:
var v1:Number=10;
v2:Number=10;
v3:Number=10;
v4:Number=10;
v5:Number=10;
otherwise, good post!
Miha,
Thanks a lot for your addition. I’ll add it to the post!
Dennis
Thanks a lot, very useful
keep up good work!
I like Actionscript - i’m fairly new to it but i find this appraoch is adding more pzazz to my sites than DHTML. Thanks for the tips and bring it on!
Actionscript is the field of search engine optimization. In the experimentation that i read its now quite search engine robots friendly. although am not 100% if this is true, what i know is that it is still not 100% crawlable by the robots. Correct me if am wrong…
Faster Integer Math - Bitwise Operators Several months ago, I stumbled upon a great resource for bitwise operators in AS3. Bitwise math is available in many languages, but this one looks at it from an AS3
I also found this one maybe it helps too
http://osflash.org/as3_speed_optimizations
thanks for your optimizations.. in all cases i like flash..
nice tips… keep working on that!
awesome tips and was very helpful to me..thanks
Hi Dennis,
Thanks for the article (via Daniel Hai’s site). I’ve posted an article containing these and many more on my site, http://www.visualharmonics.co.uk/actionscript-optimizations-resource-management/
Couldn’t find a pingback link here so thought I’d let you know. Looking forward to your comments!
-Nick
that was awesome
thanks for that helpful post
Variable instantiation - that one is weird.
I did a few more tests and they’re only faster if they’re all on the same line.
startTime = getTimer();
for(var i:int=0; i<100000; i++)
{
var v1:Number=10,
v2:Number=10,
v3:Number=10,
v4:Number=10,
v5:Number=10;
}
endTime = getTimer();
trace( “test = ” + (endTime - startTime) + “m/s ” );
takes 26 ms.
However:
startTime = getTimer();
for(var i:int=0; i<100000; i++)
{
var v1:Number = 10; var v2:Number = 10; var v3:Number = 10; var v4:Number = 10; var v5:Number = 10;
}
endTime = getTimer();
trace( “test = ” + (endTime - startTime) + “m/s ” );
gives me 9ms.
Bizarre and annoying, there isn’t a way to get the performance and readable code.
Apologies for double post - fixed less-than signs this time around!
Variable instantiation - that one is weird.
I did a few more tests and they’re only faster if they’re all on the same line.
startTime = getTimer();
for(var i:int=0; i<100000; i++)
{
var v1:Number=10,
v2:Number=10,
v3:Number=10,
v4:Number=10,
v5:Number=10;
}
endTime = getTimer();
trace( “test = ” + (endTime - startTime) + “m/s ” );
takes 26 ms.
However:
startTime = getTimer();
for(var i:int=0; i<100000; i++)
{
var v1:Number = 10; var v2:Number = 10; var v3:Number = 10; var v4:Number = 10; var v5:Number = 10;
}
endTime = getTimer();
trace( “test = ” + (endTime - startTime) + “m/s ” );
gives me 9ms.
Bizarre and annoying, there isn’t a way to get the performance and readable code.
[...] http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]
[...] http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]
Hi,I also use getTimer() to calculate the time, and run each function 50 times to get the average time.However, the results were not stable.
In addition, I run the .as in Adobe Flash CS3 Professional and Flex separately, but the results were totally different.
Hence, my question is :
Is getTimer() accurate?Are there some other ways to calculate the time?
why the results are not stable?
Which is more accuarte, Adobe Flash CS3 Professional or Flex?
Thank you.
Nice Article, I couldn’t get the var trick to work for me, in fact, I am not even sure if declaring variables inside a for loop like that makes new variables. My tests and a whole mess of other tests are posted in my optimization article http://www.stephencalenderblog.com/?p=7
Thanks,
Stephen
[...] Some Actionscript 3.0 Optimizations by Dennis Ippel [...]
Thanks. Nice Article. It has helped me.
[...] Dennis Ippel: Some ActionScript 3.0 Optimizations http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]
great resource! thanks for sharing.
for flex users here another performance issue:
Databinding with BindingUtils is 100% faster then in mxml…
More details can be found under:
http://www.screenshot.at/blog/2009/04/18/databinding-under-the-hood-part-1-performance/
[...] Dennis Ippel: Some ActionScript 3.0 Optimizations http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]
[...] 1; var val2:int = 4 << 2; var val3:int = 4 >> 1; var val4:int = 4 >> 2; } 原文http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ No Comments Read [...]
( “test = ” + (endTime - startTime) + “m/s ” )
[...] Actionscript 3 Optimization. This man tells it best: http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]
[...] Some ActionScript 3.0 Optimizations | Rozengain.com – Creative Technology Blog [...]
In regards to Variable instantiation:
After doing a lot of benchmarking on this one, the results vary. Sometimes instance 1 is faster than instance 2 and vice versa. The data that I pooled while doing this showed that it’s pretty close to 50/50 and comes down to a matter of preference.
[...] update 2010-04-09 kamijoさんのブログに追加投稿がありました。 Flash コンテンツパフォーマンス最適化 (AS3 編) このエントリーの中にリンクされているSome ActionScript 3.0 Optimizationsの最適化ネタは一度は読んでおくべきだと思います。 [...]
Performance is everything
If you have any flash files you would like to share, come upload them to Flash Source Files
[...] up of ActionScript 3.0 and Flex optimization techniques and practices Some ActionScript 3.0 Optimizations Categories: Flash As « [Today] [...]
[...] Some ActionScript 3.0 Optimizations [...]