I recently received an e-mail from asking which is faster: a DisplayObjectContainer
or a Vector
of DisplayObject
. To ask this is to question whether or not we can do better than the Flash Player’s native container of DisplayObjects
using AS3. It turns out that we can. Read on for several ways to improve on DisplayObjectContainer
‘s speed.
Archive for October, 2010
There are three main ways to access the contents of objects in AS3: the dot (.
) operator, the index ([]
) operator, and the in
operator. The first two are well known and functionally-equivalent because obj.property
evaluates to the same value as obj["property"]
. The in
operator is different as I’ve described before: it returns a Boolean
indicating whether or not the object has the given property. There are a lot of cases—error checking, for example—where we only care if an object has a property and not what that property is. So, can we improve performance by using the is
operator rather than the index or dot operators? (UPDATE: hasOwnProperty results added)
Closures are a really nice feature of AS3 (and JavaScript and AS2) and I’ve shown their performance disadvantages compared to regular methods before. Today I’ll discuss a further performance downside to closures that can slow down your code, not just the function call itself.
Today’s article is about the basic operators that make up most languages, and in particular AS3. Without them there wouldn’t be much of a language. So it would seem vitally important that we know how they perform relative to each other. Is shifting faster than adding? Adding faster than multiplying? Multiplying faster than dividing? Does the type of the operands matter? Read on for the results in high detail. Update: see my comment below for an important change to the results.