Vishwas
Vishwas

Reputation: 1541

Is it sensible to use AS3 Vectors for class instances?

Here is a simple code :

var a:Array = new Array( ins0, ins1, ins2, ins3,...., ins10000) ;
findIns( ins1500) ;

function findIns( ins:SomeInstance ) {  
    for ( var i = 0 ; i< a.length ; i++)  {
        if ( a[i] == ins ) {
            trace ( "FOUND IT");
            break; 
        }
    }
}

In the above code there is NO "int" or "string". They are instances of some complex class. So is it sensible to use Vectors in place of arrays in this case.

In my opinion it should be sensible, because instances are "numerical-memory-locations" afterall ?

Upvotes: 1

Views: 149

Answers (2)

Eduardo
Eduardo

Reputation: 8392

Yes, you are right. What gets stored inside the vector is a reference to your object, not your object itself. You can check that doing the following:

var ref:yourType = a[0];
a[0] = someOtherObjectInstance;
trace(ref.toString());

You will find that ref still points to your original object.

Upvotes: 1

laurent
laurent

Reputation: 90776

If the content of the array are all instances of the same class, then yes Vector will definitely performs better than Array.

See the documentation for more information:

  • Performance: array element access and iteration are much faster when using a Vector instance than they are when using an Array.

  • Type safety: in strict mode the compiler can identify data type errors. Examples of data type errors include assigning a value of the incorrect data type to a Vector or expecting the wrong data type when reading a value from a Vector. Note, however, that when using the push() method or unshift() method to add values to a Vector, the arguments' data types are not checked at compile time. Instead, they are checked at run time.

  • Reliability: runtime range checking (or fixed-length checking) increases reliability significantly over Arrays.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html

Upvotes: 1

Related Questions