AS3 gives you arrays and a way to sort them, but no easy way to keep them sorted as you add elements to them. Today’s article discusses an easy way to do just that: build a sorted array and keep it sorted. As a bonus, the array provides very fast searching for the elements it contains.
Posts Tagged index
One of the very nice features of AS3 (and AS2 and JavaScript for that matter) is that you can dynamically access the fields of any object. This leads to much more dynamic code since you no longer need to know what field to access at compile time. As we’ve often seen with other dynamic features, this can come at a steep cost in terms of performance. Today we’ll see just how slow accessing fields this way is to get a good idea of just how much performance we give up when using this feature.
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)