Top Ten ActionScript Performance Tips

Posted: January 28th, 2009 under ActionScript 3.0.

These are tips I have personally used and have indisputable results. Of all the things you can do to improve performance these are the most effective and efficient changes that you can make.

10. Make sure your performance problem is not a memory problem

Debugging 101 is to correctly identify the source of your problem. If you are using up all of your available RAM with references to artwork or other memory intensive structures your processor will be crippled by having to retrieve and set data from slower memory. If your application slows over time it is most likely a memory leak issue were objects you intended to delete are still being stored. The other reason to check for this issue first is that it is very easy to see how much memory that you are using, Flash does everything for you, just call System.totalMemory. Easier still, just bring up the task manager and consult your page file usage.

9. Unnecessary import statements

Not the biggest time saver unless you are generating many instances of the class or the class has a large number of function calls made to it. However, it is a very easy way to improve performance.

8. Sprites and MovieClips

You only need to use the MovieClip type if the object has multiple frames, you can export for ActionScript Library objects extending the Sprite class instead of the MovieClip class.

7. Resolve issues of promotion, unknown, or incorrect object types

ActionScript can waste a lot of time determining the type of an object if it does not understand the object type, or if ActionScript has to cast an object type. Pay close attention the object types of function parameters and types functions return as these are typical problem sources. Individually, object type compatibility changes are not going to save you that much time. In most cases this is something that you would need to be doing thousands of times to see a sizable difference in performance.

6. Unnecessary operations and function calls

Instead of dividing, multiply. Instead of calling Flash’s math class for simple things like floor and round, calculate them yourself. Rank your if statements in order of comparisons most likely to be true. In for and while statements locally store function values instead of repeated accessing them and reuse objects if possible. Similar to promotion issues, individual calls are not worth worrying about, but there can be a big difference in loops iterated several hundred times.

5. Avoid using masks

If you need to mask a square area use the display object’s ‘scrollRect’ property instead. If you need a shape that is not a square, place that shape over the object with the scrollRect property.

4. Disable events that you do not need

Dispatching events is one of the more expensive function calls that you can make. Every class that extends InteractiveObject (which encompasses every display object) generates a host of events that you most likely do not need. Take the time to set MouseEnabled and MouseChildren to false to keep your event system as clean as possible. Also, make sure to remove event listeners and onEnterFrame events when you no longer need them.

3. Limit your art assets attached to the stage

No sense in traversing render nodes if they are not actively being used. If it is an object that is not frequently visible I will completely destroy it and recreate it when it is needed again. The archetype to employ this strategy is menu systems.

2. Cache appropriate objects

Cache your most expensive objects to render (set cacheAsBitmap property to true). Caching an object stores it in memory, and if it is static, you will only have to pay to render it once. Filter effects cause objects to be cached, if many of your objects have filters applied to them they could be the cause of a memory problem. If a cached object is animated, scaling, or rotating it will be re-cached for every one of those graphic manipulations which is a heavy burden for the processor.

1. Use the right type of art asset for the job

Use vector art for your simplest objects and png or jpg file types for the more complex graphics. Rendering and graphic related function calls are the most processor intensive routines; therefore, rendering is usually where you can make the most improvement. Check out an objects memory consumption by going to File -> publish settings -> flash tab -> generate size report to get a rough idea of which assets to change.

If you are curious about other optimization techniques, want an idea of the expense of related operations, or to get a general idea of where your processor time is being spent I would suggest reading my benchmarking series.

Thanks for reading, and remember, we are all in this together.

Edit (April 4th, 2013): Long overdue (late 2011), Adobe released a comprehensive list of performance tips and general development recommendations. Epic games also has a great article of best practices with performance in mind, which is still relevant to even if you are not using Scaleform or the Unreal game engine.

Bookmark this on Delicious

6 Comments »

  • Comment by Jason Reinsvold — February 2, 2009 @ 1:45 pm

    1

    Great list by the way. I endorse this message. :) On the issue of declaring types (tip 7), I’d like to add that using the right type is key. For example, use int instead of Number for all your loops. Also another tip I would add is for operations that are executed many times per frame, store lookup vars locally (e.g. instead of iterating a loop checking index < array.length, store array.length into an arrLen:int var before the loop and check index < arrLen). Remember, every dot is a lookup operation.


  • Comment by ickydime — February 7, 2009 @ 12:49 pm

    2

    Great list Stephen. You’ve inspired me to look into changing my ScrollPanel component into using ScrollRect instead of passing in a mask.


  • Comment by Otavio — June 18, 2010 @ 9:39 am

    3

    Great tips, i ll start revising my codes right away. Man its tough to find optimization tips like this. thanks


  • Comment by Vash the Stampede — July 8, 2010 @ 1:51 pm

    4

    @Jason Reinsvold about Tip #7

    in a benchmarking blog similar to the one on this site it mentioned that uint is slower than int. so even though uint seems best since you’ll never need a negative in many for loops Flash does some weird promotion stuff which slows it down.

    @Tip #6
    the same blog mentioned the shortcut of casting to int instead of using Math.floor

    Great blog, keep up the good work!


  • Comment by BigAl — May 26, 2011 @ 7:18 am

    5

    Very useful!

    Also, be sure to check out this recent article on AIR 2.6 / Flash 10.3 performance:
    http://blog.flexwiz.net/top-10-performance-killers-in-your-air-application/


  • Pingback by Flash Game Development Inter-web mash up: Feb 9, 2009 « 8bitrocket.com — August 25, 2013 @ 4:14 pm

    6

    [...] Helpful hints from the field- Ickydime solves the problem of stopping FLV Playback's progressive download.- Stephen Calender's Top 10 Flash Performance Tips [...]


RSS feed for comments on this post. TrackBack URL

Leave a comment