If-else trees have some of the best performance of any conditional code, including if-else
ladders, the ternary (? :
) operator, and the switch
statement. But how do they stack up against the O(1) lookups that Object
and Dictionary
offer us AS3 programmers? Today’s article finds out!
Posts Tagged dictionary
Today’s article is about three totally unrelated discoveries I’ve recently made in AS3. These answer three questions I’ve recently had. Should you cache the object you’re looping over with a for-each
loop as a local variable? Can you clear a Dictionary
or Object
with a for-in
loop? Is it faster to write your own version of Vector.indexOf
? All of these questions are answered in today’s article!
AS3 has three kinds of loops—for
, for-in
, and for-each
—but which is fastest? I attempted to answer that question about three years ago, but the article is in dire need of a followup as many version of Flash Player have been released since then and the question is core to our everyday lives as AS3 programmers. So which type of loop is fastest in 2012?
Behind the scenes Array
holds its values in two ways: a densely-packed array at the beginning and a sparsely-packed map after that. This means it can be used as a map where the keys are indexes and not take up a huge amount of wasted space. Dictionary
can also have int
keys. Which is faster? Today we’ll find out!
One of the advantages of using Dictionary
instead of Object
when mapping key-value pairs is that you can use whatever type of key you want, not just a String
. However, a recent comment points out that the keys are still checked with the loose equality operator (==
) and you can therefore get clashes like 4 == "4"
. For today’s article, I’ve written a TypesafeDictionary
class that allows you to overcome this limitation. Read on for the implementation, performance testing, and more.
The Dictionary
class provides perhaps the most useful support for weak references—and therefore garbage collection control—in the AS3 Flash API. However, due to a subtle error in its documentation, you may inadvertently be leaking a lot of memory. Today’s article shows you how this can happen and how you can easily fix the leak.
Sometimes you need to map a key to many values, but AS3 has no built-in data structure for this purpose. Dictionary
and Object
are suitable one-to-one maps, but there’s been no one-to-many support until now. Read on for my own one-to-many class—MultiMap
—as well as performance testing and analysis.
XML is widely used in AS3 applications for everything from simple configuration files to complex networking protocols. AS3 even includes 10 operators in its syntax specifically to make XML easier to work with. This often leads to AS3 developers loading XML documents and then just leaving them as an XML
objects. XML’s performance begins to seep into the rest of the AS3 application. Today we look at just how much this can slow down our apps.
AS3 has an interesting feature that is sometimes used to great effect: dynamic classes. These classes can have fields added and removed to them and used like any other field. You can even make your own dynamic classes with the dynamic
keyword. However, all of this fancy functionality comes at a steep performance cost. How steep? Read on to see just how steep.
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)