Some ActionScript 3.0 Optimizations

Posted by Dennis on May 1, 2007 in ActionScript, Flash, Flex36 comments

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:



Tags: , , ,


36 comments

» Comments RSS Feed
  1. I remember Ted Patrick saying something about the overhead of using int over Number typing….hit him up about some of his knowledge.

  2. Very helpful - thank you!

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

  4. Useful links, thanks!
    Dennis

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

  6. Miha,
    Thanks a lot for your addition. I’ll add it to the post!
    Dennis

  7. Thanks a lot, very useful

    keep up good work!

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

  9. 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…

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

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

  12. nice tips… keep working on that!

  13. awesome tips and was very helpful to me..thanks ;)

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

  15. that was awesome ;)

  16. thanks for that helpful post

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

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

  19. [...] http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]

  20. [...] http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]

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

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

  23. [...] Some Actionscript 3.0 Optimizations by Dennis Ippel [...]

  24. Thanks. Nice Article. It has helped me. ;)

  25. [...] Dennis Ippel: Some ActionScript 3.0 Optimizations http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]

  26. 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/

  27. [...] Dennis Ippel: Some ActionScript 3.0 Optimizations http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]

  28. [...] 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 [...]

  29. ( “test = ” + (endTime - startTime) + “m/s ” )

  30. [...] Actionscript 3 Optimization. This man tells it best: http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-optimizations/ [...]

  31. [...] Some ActionScript 3.0 Optimizations | Rozengain.com – Creative Technology Blog [...]

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

  33. [...] update 2010-04-09 kamijoさんのブログに追加投稿がありました。 Flash コンテンツパフォーマンス最適化 (AS3 編) このエントリーの中にリンクされているSome ActionScript 3.0 Optimizationsの最適化ネタは一度は読んでおくべきだと思います。 [...]

  34. Performance is everything

    If you have any flash files you would like to share, come upload them to Flash Source Files

  35. [...] up of ActionScript 3.0 and Flex optimization techniques and practices Some ActionScript 3.0 Optimizations Categories: Flash As « [Today] [...]

  36. [...] Some ActionScript 3.0 Optimizations [...]

Leave a comment