Seven tips about performance optimization in Actionscript 3

This post is a list of tips and tricks for tunning performance in Actionscript 3. Keep in mind the following quote while reading this:

“Premature Optimization is the root of all evil.” – Hoare and Knuth

1.- Type Everything.
Every variable declaration, every function return value, loop counter, every function parameter must be typed. Avoid things like this:


var foo ;

var foo2:Object ;

var foo:* ;

Typing optimizes the bytecode.2.- Avoid implicit types conversion of numeric types. This means:

var foo:int = 10 ;

Math.sqrt( foo );

That is an implicit conversion, square root function receive a Number for parameters. Sometimes just using Number is more efficient.3.- If you are going to access a property, variable, or a function value more than a few times, store that value in a local var:

var person:Person;

if (person.firstName == “Matt” || person.firstName == “Ted” ||

person.firstName == “Alex”)...

var fname:String = person.firstName;

if (fname == “Matt” || fname == “Ted” || fname == “Alex”)

Imagine if that query is in a loop, every dot represents a lookup
Other example of the same, maybe less trivial:

for(var k:int=0; k<    testArray.length; k++)
{
	trace(testArray&#91;i&#93;);
}&#91;/sourcecode&#93;
Here we are calling the length of the array in every loop, we can change it for something like this:
&#91;sourcecode language='jscript'&#93;
var l:int = testArray.length;

for(var i:int=0; i<    l; i++)
{
	trace(testArray&#91;i&#93;);
}&#91;/sourcecode&#93;
In the second case we are calling testarray.length just once for all the iterations.<b>4.- Use int for index array instead of Number. The array indexing is faster with ints.</b><b>5.- Avoid variable declaration inside loops, if you have something like this:</b>

for(var j:int=0; j<    1000000; j++)

{

var loopvar:Number = Math.random();

}&#91;/sourcecode&#93;
Change it for this:
&#91;sourcecode language='jscript'&#93;
var loopvar:Number = Math.random();

for(var j:int=0; j<    1000000; j++)

{

loopvar = Math.random() ;

}&#91;/sourcecode&#93;
Make your var declaration outside the loops.<b>6.- Cast instances, while reading from Array.</b>
The Actionscript array can store any type of data, so before you call a method or access a property of an instance inside the array, don't do it directly from the array, first store the instance in a typed variable.
Instead of this:

testArray[i].x

Do this:

var auxPoint:Point ;
auxPoint = testArray[i] as Point ;

This other option is valid, but I made a test and the previous way using the “as” operator is faster than this:

var auxPoint:Point ;
auxPoint = Point( testArray[i] ) ;

7.- Bit operation are faster than normal operators.
If you have intensive math operations like this:

var val1:int = 4 * 2;
var val2:int = 4 * 4;
var val3:int = 4 / 2;
var val4:int = 4 / 4 ;

Can be changed for:

var val1:int = 4 << 1;
var val2:int = 4 << 2;
var val3:int = 4 >> 1;
var val4:int = 4 >> 2;

7.- Watch your Listeners.
“A game sprite subscribes to its own enterFrame event. In every frame it moves, the application performs some calculations to determine its proximity to other game elements. In ActionScript 3.0, even after you remove the sprite from the display list and null all references to it, the application continues to run that code in every frame until it is removed by garbage collection. You must remember to explicitly remove the enterFrame listener when the sprite is removed.”
If you don’t remember to remove the listener of this sprite you will be leaking memory.
You have to solutions available. The first one is remember to remove your listeners when you have done to use them, so reference to the object is eliminated and the garbage collector can take it in the next sweep.
The second is use weak reference when you register your event listeners. A weak reference is one that is not counted by the Garbage Collector to decide if that object must be collected. This means that if the only reference remaining to an object are weak the object will be marked for collection anyway and that piece of memory will be freed.
We can do this with the function addEventListener, the fifth parameter is a flag called useWeakReference by default is in false, you should set it to true:

stage.addEventListener(Event.ENTER_FRAME, YourLoopFunction,false,0,true);

This is an example with all values set by default except the useWeakReference, the last parameter set to true.Sources:
Matt Chotin’s presentation at http://www.adobe.com/devnet/flex/articles/as3_tuning.html

Rozengain’s blog : http://rozengain.com/?postid=35

Andre Michelle’s blog: http://blog.andre-michelle.com/2005/as3-optimations-suggestions

Daniel Hai’s Blog: http://www.danielhai.com/blog/?p=55

http://www.adobe.com/devnet/flashplayer/articles/resource_management.html

Advertisements
Explore posts in the same categories: Actionscript

Tags: , , ,

You can comment below, or link to this permanent URL from your own site.

One Comment on “Seven tips about performance optimization in Actionscript 3”


  1. Great programming tips, I found some more http://www.stephencalenderblog.com/?p=7

    Love the Hoare and Knuth quote!

    Best of Luck,

    Stephen


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: