views:

73

answers:

3

Can anybody say me what is faster: Array or ArrayList? (ActionScript3)

I tried to find a page about this but didn't find anything. Thank you.

+6  A: 

The ArrayList class is a simple implementation of IList that uses a backing Array as the source of the data. Items in the backing Array can be accessed and manipulated using the methods and properties of the IList interface. Operations on an ArrayList instance modify the data source; for example, if you use the removeItemAt() method on an ArrayList, you remove the item from the underlying Array.

Apparently ArrayList class wraps an Array object - hence a plain Array would be faster than an ArrayList object.

Amarghosh
+1  A: 

Array is probably slightly faster or they are equal. All an ArrayList is, is an implementation of iList that uses an... Array as a backing object.

Tegeril
+2  A: 

As already stated, Array is faster. Actually it is orders of magnitude faster.

The equivalents of array access are getItemAt and setItemAt.

Implementation:

public function getItemAt(index:int, prefetch:int = 0):Object
{
    if (index < 0 || index >= length)
    {
        var message:String = resourceManager.getString(
            "collections", "outOfBounds", [ index ]);
        throw new RangeError(message);
    }

    return source[index];
}

and:

public function setItemAt(item:Object, index:int):Object
{
    if (index < 0 || index >= length) 
    {
        var message:String = resourceManager.getString(
            "collections", "outOfBounds", [ index ]);
        throw new RangeError(message);
    }

    var oldItem:Object = source[index];
    source[index] = item;
    stopTrackUpdates(oldItem);
    startTrackUpdates(item);

    //dispatch the appropriate events 
    if (_dispatchEvents == 0)
    {
        var hasCollectionListener:Boolean = 
            hasEventListener(CollectionEvent.COLLECTION_CHANGE);
        var hasPropertyListener:Boolean = 
            hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE);
        var updateInfo:PropertyChangeEvent; 

        if (hasCollectionListener || hasPropertyListener)
        {
            updateInfo = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
            updateInfo.kind = PropertyChangeEventKind.UPDATE;
            updateInfo.oldValue = oldItem;
            updateInfo.newValue = item;
            updateInfo.property = index;
        }

        if (hasCollectionListener)
        {
            var event:CollectionEvent =
                new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
            event.kind = CollectionEventKind.REPLACE;
            event.location = index;
            event.items.push(updateInfo);
            dispatchEvent(event);
        }

        if (hasPropertyListener)
        {
            dispatchEvent(updateInfo);
        }
    }
    return oldItem;    
}

There's a LOT of calls and checks involved here. Please note, that _dispatchEvents == 0 is true by default (unless you disableEvents), thus writing in fact is an immense operation.

However ArrayList does provide a lot of feature, that are usefull within flex. A good compormise is to grab the underlying Array (accessible as ArrayList::source), peform your operations, and then reassign it (supposing you have listeners observing that Array).

Also, if you go with Flash Player 10, then Vector will outperform Array.

greetz
back2dos

back2dos
+1 for that :-)
Amarghosh