Flash Workshop in Bay Area →← Debugging Tool

Flash Performance Tips Part I

Optimizing Flash sites to run efficiently is 93% perspiration, 6% electricity, 4% evaporation, and 2% butterscotch ripple. Despite many coders’ frustrations over the performance of the player, there are ways you can make your site run more efficiently.

Here’s a couple of tricks we use on a daily basis to keep things running smooth. Nothing fancy or complicated, but guaranteed success.

UPDATE: Tera at trick7 posted a Japanese translation for our fine friends on the other side of the Pacific. We think that’s pretty cool. Check it out here.

Masks Are Bad
Well, they’re not all bad. Masks can be exceptionally useful, as we all know by now. They’re the #1 performance killer though. When you mask something, you force the player to decide what needs to be rendered and hidden every single frame. So how do you get around without masks? With a little bit of patience and tricky layering (such as making the background the foreground with a giant hole cut out for where your viewable area is) you’ll be able to get the same end result without the performance hit.

Alpha PNGs and Video
Same deal as the masks. Sometimes its unavoidable, but you’re still asking the player to figure out what to render under the alpha. Sometimes we’ll make an alpha video at half size and then scale it in Flash. You’d be surprised how good it still looks.

Oh, and with regards to alpha video: Try experimenting with PNG sequences in place of the alpha video. Video usually performs a little better, but its always worth looking into.

Frame Rate
Despite all discussions otherwise, there is no magic framerate. We use 25 or 30 because (as far as I know) we like it best. At some point we tested and determined one was slightly better than the other, but generally speaking this is not going to be the primary cause of a site running slow. I wouldn’t generally advise going higher than 30 though, just because you’re asking the player to render an awful lot awfully fast…

cacheAsBitmap and BitmapData
Where possible use cacheAsBitmap to rasterize vector items. You’ll force Flash to draw the symbol one time and then never again. On the flip side, if you’re scaling or rotating a symbol NEVER set it to cacheAsBitmap. Then you force Flash to render AND recapture the raster every frame, making the process slower instead of faster.

With sites like the Da Vinci Code and Nike Air, we would take a dynamic screenshot of the section, draw it into an empty movieclip and then manipulate the screenshot to animate it out. This is far, far faster than animating many elements out, or animating over top of many elements. I highly recommend this practice.

_visible is better than _alpha
_alpha = 0 and _visible = false are totally different things. Alpha determines the opacity of a clip. Visible determines whether or not the clip should actually be rendered by the player. If you’re setting something all the way invisible, use the _visible property.

onEnterFrame and setInterval
When you’re through with these processes, clear them from memory with onEnterFrame = null; and clearInterval(myInterval); respectively. Leaving these around when you’re not using them is like leaving the telephone off the hook when you’re done with a call.

Pre-define your math
Got a sine wave you’re about to draw? Is it the same sine wave every time? Hard code the numbers into an array. By doing the math for Flash, you’re saving some complex processes in advance. I even experiment with using a Tween that I nextFrame() thru to get all of the entries into an array before hand.

Silent Sound
We use this as a last resort, but its a trick worth mentioning. By putting a silent sound file on a seperate layer for the duration of your timeline, setting it to stream and looping it, the player will automatically drop frames to keep up with the sound.

Got any other good ones? I’m sure I didn’t list them all here.

December 8th, 2006  by Stephen  /  93 Comments

Comments on “Flash Performance Tips Part I”

  1. Hey there - thanks for this.

    Can you tell me if -

    onEnterFrame = null

    is any different to

    delete this.onEnterFrame

    ??

    Battleman on December 9th, 2006 at 4:37 am
  2. great! just great!

    I am searching all over the time for ressources on how to optimize the performance of my flash-apps…

    and your tips/tricks are pretty neat for sure!

    waiting patiently for more parts of this article ;-)

    til then and your blog rocks,
    lorenz

    Lorenz on December 9th, 2006 at 6:38 am
  3. Here’s another important one:

    Sealed Objects
    Always, always, ALWAYS define your own data types instead of using ‘Object’ whenever possible. Further more, make sure your object’s are sealed (not-dynamic). In situations where properties of an object are getting called frequently (such as in a loop or enter frame/interval) it is sooo much faster if Flash already knows what properties exist in the object rather than having to check a dynamic object each time to see what properties it has.

    Ryan Taylor on December 9th, 2006 at 12:37 pm
  4. and this one too:
    _visible is good but…
    A common misconception of using ‘_visible’ is that when set to false, it does know to stop rendering that object (which is good), however anything going on inside the object (such as code or motion tweens) still continues to happen. So for example, if you have a movie clip with a bunch of child movie clips within it, and some of them are components with onMouseMove events and stuff, the events are still getting triggered everytime you move the mouse. There is certainly a time and a place to use ‘_visible’, like to define a hit area and then hide the clip. Just make sure you are careful with how you use it and always make sure to either call down through all objects to disable code and stop tweens and then hide everything, or simply remove the clip from the stage all together and then reload it next time you need it.

    Ryan Taylor on December 9th, 2006 at 12:48 pm
  5. Rather than using a mask to crop a viewport of content use scrollrect.

    Quoting docs:

    The scrollRect property allows you to quickly scroll movie clip content and have a window viewing larger content. Text fields and complex content scroll much faster, because pixel level copying is used to scroll data instead of regenerating the entire movie clip from vector data. To see the performance gain, use scrollRect in conjunction with a movie clip that has cacheAsBitmap set to true.

    Julian on December 9th, 2006 at 6:30 pm
  6. Excellent!
    Thank you spaceship!

    Savvas Malamas on December 9th, 2006 at 8:11 pm
  7. When I code onEnterFrame handlers I always use “delete this.onEnterFrame” or “delete .onEnterFrame” to destroy the handler when I’m done with it. I noticed that you recommend “onEnterFrame = null” instead. Would that not simply set its pointer to null and orphan the object in memory or does Flash garbage collect orphaned objects? And if it does, wouldn’t it be more efficient to destroy the object explicitly rather than let the player figure out that it needs to do that?

    Dave Willis on December 9th, 2006 at 8:22 pm
  8. great tips guys, thanks for posting stuff like this up. it’s always useful.

    Matt Przybylski on December 10th, 2006 at 2:16 pm
  9. […] Big Spaceship put up some of their performance tips that they use in house on some of their projects.  Some pretty neat stuff in there, I didn’t know about a lot of those. […]

    reintroducing.com Blogging Receptacle » Blog Archive » Flash Performance Tips on December 10th, 2006 at 2:20 pm
  10. I recommend onEnterFrame = null. It does the same thing as delete (sets the onEnterFrame for garbage collection, thus terminating the process), but on top of that in AS3 “delete onEnterFrame” will be/should be invalid.

    I’ve migrating from using delete to setting a variable to null. That in itself should be a tip. :)

    Jamie on December 11th, 2006 at 11:51 am
  11. Thanks for this helpful tips guys!

    @Julian:
    With scrollRect propertry pixel level is used, but on the other hand with greater text areas (2880 px if im not wrong) cacheAsBitmap does not work.

    scrollRect still has the best performance for a sroll pane with smaller areas anyway.

    Özgür ALTAY on December 11th, 2006 at 2:49 pm
  12. […] The production team from Big Spaceship has blogged some tips and best practices for optimizing the performance of your Flash sites and applications. Good Read. read more | digg story […]

    disassembled formulas » Blog Archive » Flash Performance Tips from Big Spaceship on December 11th, 2006 at 4:25 pm
  13. Uh - this one is a keeper! Thanks!

    Can you elaborate on the following:

    With sites like the Da Vinci Code and Nike Air, we would take a dynamic screenshot of the section, draw it into an empty movieclip and then manipulate the screenshot to animate it out. This is far, far faster than animating many elements out, or animating over top of many elements. I highly recommend this practice.

    I don’t quite get it ;-)

    Mikkel Bech on December 11th, 2006 at 4:48 pm
  14. @Mikkel Bech: The technique might be a post unto itself, but I’ll try and explain further here.

    We’ve got an empty movieclip sitting at 0, 0 above our “section” (I’ll call it mySectionClip for now). When the user chooses to navigate elsewhere, we create a new BitmapData, drawing mySectionClip into the empty movieclip. Now that we have this screenshot captured, we can remove or unload mySectionClip and effectively eliminate any processes while displaying a transition to the next section.

    The end user sees mySectionClip freeze in place and we’ve freed up the processor exclusively for what they’re seeing now, not what was going on right before they decided to navigate away!

    Jamie on December 11th, 2006 at 5:40 pm
  15. I heard 31fps & 21 fps were optimal framerates for cross platform?

    Biffer on December 11th, 2006 at 6:09 pm
  16. Biffer, where did you hear this?

    c.johnston on December 11th, 2006 at 6:16 pm
  17. thanks, such an issue

    christophe marand on December 12th, 2006 at 4:20 am
  18. Biffer,

    If I remember correctly, the 31fps practice was established to workaround issues on the Mac version of the Flash 6 Player. I believe this issue has been resolved in subsequent releases of the Flash Player and is therefore no longer necessary.

    Noel on December 12th, 2006 at 3:33 pm
  19. A working example or something would be cool for the “dynamic screen shot” functionality. Sounds really interesting and I can imagine it boosting performance quite a bit. Bigspaceship rocks — as always.

    Mike on December 13th, 2006 at 4:50 am
  20. @Mike:

    I’ll draw up an example and post the source as soon as I get the chance to.

    Jamie on December 13th, 2006 at 1:00 pm
  21. Thanks dude.

    Mike on December 13th, 2006 at 1:58 pm
  22. […] Big Spaceship has a wonderful labs site, and they just posted several great Flash optimization tips. While the list isn’t comprehensive, the tips are a great reminder about the little things you can be doing to get your Flash creations to run smoother. […]

    jonnymac blog » Flash Performance Tips on December 14th, 2006 at 3:57 pm
  23. For heavy PNG sequences, I use the following JSFL script to lower the quality of images that contain a lot of movement (I usually leave the first and last 3-5 frames at a higher quality). Probably doesn’t save a whole lot of processor speed, but it does reduce your file size:

    http://chattyfig.figleaf.com/pipermail/flashcoders/2006-August/172030.html

    Kevin Sweeney on December 14th, 2006 at 4:34 pm
  24. […] NY based creative agency and after some digging around, I assume that the posts by ‘Caleb’ is the Caleb who used to work in Yahoo! US. http://labs.bigspaceship.com/blog/?p=16#more-16 […]

    andrew lloyd :: creative design » Blog Archive » Flash Optimisation on January 4th, 2007 at 5:34 am
  25. hey guys, great work, and great tips! Thanks. I’ve also read that setInterval at a short duration is more efficient than onEnterFrame. Also, I have a question regarding flash performance when nesting complex movieclips. I have a movieclip that is a parent for the entire flash movie (it loads a swf in it). The parent of that holder mc has various animations going on inside it. I’ve noticed when I do this the player lags considerably but only after some time browsing. I’ve debugged and ensured code is being cleaned up properly. Have you noticed anything like this when nesting complex mcs? Thanks again, and you guys ROCK!

    allan on January 10th, 2007 at 3:51 pm
Next →