Marty
Marty

Reputation: 39456

Is there any advantage in using Vector.<Object> in place of a standard Array?

Because of the inability to create Vectors dynamically, I'm forced to create one with a very primitive type, i.e. Object:

var list:Vector.<Object> = new Vector.<Object>();

I'm assuming that Vector gains its power from being typed as closely as possible, rather than the above, but I may be wrong and there are in-fact still gains when using the above in place of a normal Array or Object:

var list:Array = [];
var list:Object = {};

Does anyone have any insight on this?

Upvotes: 4

Views: 2888

Answers (3)

Jack Wester
Jack Wester

Reputation: 5630

You will not gain any benefits from Vector.< Object > compared to Array or vice versa. Also the underlying data structure will be the same even if you have a tighter coupled Vector such as Vector.< Foo >. The only optimization gains will be if you use value types. The reason for this is that Ecmascript will still be late binding and all reference objects share the same referencing byte structure.

However, in Ecmascript 4 (of which Actionscript is an implementation) the Vector generic datatype adds bounds checking to element access (the non-vector will simply grow the array), so the functionality varies slightly and consequently the number of CPU clock cycles will vary a little bit. This is negligible however.

Upvotes: 6

Daniel
Daniel

Reputation: 35684

One advantage I've seen is that coding is a bit easier with vectors, because FlashDevelop (and most coding tools for as3) can do code hinting better. so I can do myVector. and see my methods and functions, array won't let you do that without casting myArr[2] as myObject (thought this kind of casting is rumoured to make it faster, not slower)

Array's sort functions are faster however, but if it is speed you're after, you might be better served by linked lists (pending the application)

I think using vectors is the proper way to be coding, but not necessarily better.

Upvotes: 2

Jonathan Dunlap
Jonathan Dunlap

Reputation: 2631

Excellent question- Vectors have a tremendous value! Vector. vs Array is a bad example of the differences though and benchmarks may be similar. However, Vector. vs Array is DEFINITELY better both memory and processing. The speed improvement comes from Flash not needing to "box" and "unbox" the values (multiple mathematical operations required for this). Also, Array cannot allocate memory as effectively as a typed Vector. Strict-typing collections are almost always better.

Benchmarks: http://jacksondunstan.com/articles/636 http://www.mikechambers.com/blog/2008/09/24/actioscript-3-vector-array-performance-comparison/

Even .NET suffers from boxing collections (Array): http://msdn.microsoft.com/en-us/library/ms173196.aspx

UPDATE:

I've been corrected! Only primitive numeric types get a performance enhancement 
from Vectors. You won't see any improvement with Array vs Vector.<Object>.

Upvotes: 0

Related Questions