ActionScript 3 optimization techniques

If you attended my session about bytecode inlining at FITC Toronto you might remember that I said I have a document covering a lot of common optimization techniques.

After talking to so many of you and getting nice feedback I decided to release it in a state which is a work in progress.

My main idea is still to put this into a Wiki so everybody can share his or her knowledge. Since I am pretty busy the next weeks I do not know when this will happen so for now I hope you get some use out of the PDF.


  1. Posted Apr 26, 2008 at 10:22 pm | Permalink

    Thanks Joa, will compare it to my own experience.

  2. Lenny
    Posted Apr 27, 2008 at 5:10 pm | Permalink

    A wiki on as3 optimisation is indeed a great idea. Two thumbs up!

  3. Posted Apr 27, 2008 at 5:32 pm | Permalink

    Thanks Joa.

  4. Erik
    Posted Apr 28, 2008 at 1:31 am | Permalink

    Hello, very nice that you gather all this stuff.

    I was looking at 2.3 Casting and wondered why the ‘as’ operator was not mentioned. So I run a few (very simple) tests.

    As it turns out using the as operator on my machine with 9.0.115 is the fastest. Strange enough, using a cast slower on my machine then doing nothing.

    From fastest to slowest (in my simple setup):

    (_a[i] as Point).x = 2;
    _a[i].x = 2;
    Point(_a[i]).x = 2;

    Could you include the ‘as’ in the tests and maybe check if you correctly marked the casted one as being faster then doing nothing?

    Greetz Erik

  5. Posted Apr 28, 2008 at 10:05 am | Permalink

    Hi Erik,

    there is a simple difference between Cast(obj) and obj as Cast. The “as”-cast will convert your object to null if the type does not match. For instance trace((new Point as MovieClip) == null); will return true. Doing the same like this trace(MovieClip(new Point)); will throw an exception.

    Now it is very important how you test and which player you are running. The “as”-cast will work faster in the debug version of the player because there is much more overhead for exception handling. But the simple Cast(obj) is about 7 times faster in the release player.

    That is the reason why I simply do not mention it at all.

    And I think there has to be something wrong with your test since it is already common knowledge that _a[i].x is slower for a couple of reasons than Point(_a[i]).x.

    Maybe you could paste what and how you tested.



  6. Erik
    Posted Apr 28, 2008 at 1:21 pm | Permalink

    Ahh, I offcourse only have a debug player installed, hehe.

    That must be the reason. If Cast() would be about 7 times faster it would be the clear winner.

    Thanks for your good explaination.

    Greetz Erik

  7. Posted Apr 30, 2008 at 12:19 am | Permalink


    Awesome post! There’s definitely some good detailed information in that PDF. One thing that really struck me is the test code you put in to get around the millisecond limitation of the getTimer() call.

    Btw, in true wiki and community spirit, here’s the link to some of the research I’ve done on my end regarding matter. (Along with plenty other reference citations such as Matt Chotin/Ted Patrick’s talk which my research was built upon.)

    Thanks again for the outstanding materials!

  8. Posted May 22, 2008 at 7:12 am | Permalink

    Thanks for releasing this Joa. This will be a real help.

  9. Posted May 23, 2008 at 9:20 pm | Permalink

    I’m getting the same results as Erik, and I’ve tried both the debug player and the non-debug player (on a PowerPC Mac).

    On my machine, casting with a function at a million iterations takes 1500 ms, not casting takes 1000 ms, casting with “as” takes 500 ms, and assigning to a typed variable without an explicit cast takes 250 (!) ms… give or take up to 100 ms for each test.

    Here is my test: (uncomment the technique that you want to try)

    var i: int = 0;
    var iterations: int = 1000000;
    var arr: Array = new Array();
    for (i=0; i<iterations; i++) arr[i] = new Point(i,0);

    var prevTime: int = getTimer();
    for (i=0; i<iterations; i++) {
    /* Explicit casting with a function: */

    /* No casting, access variable with string: */

    /* Explicit casting with ‘as’: */
    //(arr[i] as Point).x++;

    /* No explicit casting, assign to variable then access member: */
    //var p: Point = arr[i];
    var curTime: int = getTimer();
    var delta: int = (curTime – prevTime);
    print(delta+””); // call some function to print a string

    Are your results different? Should I be trying something other then incrementing a member variable?

  10. Posted May 25, 2008 at 11:29 pm | Permalink

    Hey, I did some more tests and I think I might see where you got your results from. It seems that arithmetic expressions are ambiguously typed before compile time, but can be strongly typed after compile time using what looks like casting, but doesn’t actually result in any casting at runtime.

    var i:int;
    var j:int;
    arr[i+j]; // the expression returns a Number, which is used to as an array index
    arr[int(i+j)]; // the expression returns an int, without any runtime casting. Fastest.
    arr[(i+j) as int]; // the expression returns a Number, but then is cast at runtime to int. This version is significantly slower, because it’s the only one that casts at runtime.

    If, however, we use an array index that is unambiguously a Number, casting it to int before using it isn’t any faster.

    var n: Number;
    arr[n]; // The array internally knows how to cast the Number to int.
    arr[int(n)]; // This casts the same as the above line, except explicitly.
    arr[n as int]; // This is a slightly slower way cast from Number to int for some reason.

    What I said in my previous comment about casting objects AFTER they are retrieved from an array are still valid.

  11. Posted May 28, 2008 at 6:02 pm | Permalink

    Thanks Joa,
    i’ve used your pdf more times than I can remember now.
    recently used it to do some real time edge detection (can see it here:
    (currently requires a webcam)
    Improved the speed of the algorithm hugely by changing from 2 for loops and using bitmapData.getPixel to a while loop with a byteArray. Couldnt belive how much difference that made!

  12. Posted Jul 15, 2008 at 10:01 pm | Permalink

    great paper!
    i just want to point out that try catch is only in the case slower when you fall into the catch block. if your sprite object exists, the time is nearly the same as the check against null.

    but i agree that try catch should not be used for situations where a check against null could also be set. i think in this cases a test against null is clearer then try/catch.

    here are my test result:
    // s != null so try block is executed; n= 1000000:
    // with try catch: 305
    // test with null: 292

    // s == null so catch block is executed; n= 100000:
    // with try catch: 1623
    // test with null: 10

  13. Rob
    Posted Jul 16, 2008 at 3:09 am | Permalink

    Awesome doc, I can’t wait to see those ‘To Do’s filled in.

    For anyone interested, I squeezed a couple more ms out of the 2D loop by replacing the boolean with an increment/decrement variable and using strict equals. It’s about 30ms faster on a 2000×2000 image.

    color = bitmapData.getPixel(x, y);
    x += i;
    if(x === width){
    if(++y === height) break;
    i = -1;
    } else if (x === 0){
    if(++y === height) break;
    i = 1;

  14. Posted Jul 24, 2008 at 5:14 pm | Permalink

    Thanks for this, Joa – very helpful! :)

  15. Posted Feb 7, 2009 at 5:52 pm | Permalink

    Great Stuff,

    Few people really know how to do high quality benchmarks. I tested your findings and many more optimizations myself, I hope that you and your readers find it helpful



  16. Rohit Sharma
    Posted May 5, 2009 at 8:34 am | Permalink

    Thanks for such good stuff. It was an eye opener for me. :)

    I have one question also. Can you comment upon the usage of “trace” statements in terms of memory used. Does using too many trace statements inside multi-player games swf can cause any adverse affect?


  17. Posted May 5, 2009 at 11:05 am | Permalink

    You should use conditional compilation for your trace statements.
    Have a look at

  18. Posted Oct 1, 2009 at 7:37 pm | Permalink

    @joa you win at life. I refer to your optimization postings all the time :) Thanks!

18 Trackbacks

  1. […] reference for any serious flash programmer link to the original blog post This entry was posted on Wednesday, May 14th, 2008 at 5:21 pm and is filed under actionscript, […]

  2. […] Joa Ebert hat übrigens angefangen ein PDF zu Optimierungstechniken zusammenzustellen. Dieses hat er vor ein paar Wochen in seinem Blog zur Verfügung gestellt: ActionScript 3 optimization techniques » […]

  3. […] dig at kigge på hans egen blogpost om emnet, hvor særligt kommentarer bliver særdeles nørdede: ActionScript 3 optimization techniques Tags: actionscript 3, performance Del:These icons link to social bookmarking sites where readers […]

  4. […] […]

  5. […] through Keith Peters‘ articles, I just found one that has a comment with a link to one of Joa Ebert’s articles. There, you’ll find another link to a pdf document containing a list of optimization […]

  6. By science&code » Actionscript optimization on Aug 2, 2008 at 5:39 pm

    […] Since the introduction of actionscript and the beginning of game development with flash (and, more recently, with Flex oder similar tools) performance was always a big topic in the game developer community. A common source for benchmarks are the oddhammer actionscript performance tests which give a nice overview of actionscript behavior. Sadly, no benchmark is available for AS3.0; but there are enough resources out there dealing with the topic. […]

  7. […] reading:Joa Ebert on optimizationSpeed tests over on OS Flash Posted by Rob Filed in AS3 Speed Benchmarking, Actionscript 3.0, […]

  8. […] started an ActionScript Wiki at with most of the articles from my old optimizations paper and some new […]

  9. […] ActionScript 3 optimization techniques […]

  10. […] can also be found at Joa Ebert’s blog in much more […]

  11. By ActionScript 3.0 Benchmark | Flash-Square on Jun 22, 2009 at 1:13 am

    […] ActionScript 3 optimization techniques […]

  12. By AS3 Code Optimisation : FranckyInfo's WeBlog on Jan 11, 2010 at 1:40 am

    […] ActionScript 3 optimization techniques […]

  13. […] ActionScript 3 optimization techniques […]

  14. By Pingback Optimizer | on Oct 3, 2010 at 12:27 am

    […] Page Free Space: Paul White : Inside the Optimiser: Constructing a Plan – Part 1SEO Guy Says Search Engine Optimization SEO Is Dead | Omni Marketing – Los Angeles Internet Marketing – Online Marketing SEO CompanySimple Strategy Be Profitable | Comment TrackBack PingBack DO-Follow StumbleShadows of epiphany WordPress and Trackbacks and Pingbacks | Reviews of The Best WordPress Plug Inspolygonal labs Do-It-Yourself SEO – Carnavia BlogActionScript 3 optimization techniques […]

  15. […] executing these optimization techniques shortly after going through the complete course material.Are you currently applying effective optimization techniques on your business website today? If not,… If you are thinking of going with the former, here are the reasons why you should prefer online SEO […]

  16. […] 331-3331 Is your Organization being identified on-line? Discover out at: Post by Jessica Thomson optimization is the process to get your business site ranked higher in the T…l noticed by possible customer base. An Search engine optimisation agency analyzes the relationship […]

  17. By optimization techniques on Jun 13, 2012 at 5:10 pm

    […] ActionScript 3 optimization techniques «… If you attended my session about bytecode inlining at FITC Toronto you might remember that I said I have a document covering a lot of common optimization techniques. […]

  18. By pro_flash | Annotary on Feb 2, 2013 at 12:00 pm

    […] Share       3 minutes […]