Some ActionScript 3.0 Optimizations

I’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:

  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter

49 thoughts on “Some ActionScript 3.0 Optimizations

  1. miha says:

    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!

  2. 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!

  3. Malou Wang says:

    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…

  4. 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

  5. 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.

  6. Anonymous says:

    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.

  7. Annie says:

    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.

  8. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>