Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Flash Player 10 with Vectors, why should you use Arrays anymore?

Is there a reason to stick to Arrays as the default list data structure in AS3 if you are targeting Flash Player 10? Why not use Vectors (typed arrays) as default throughout your program since they are:

  • faster
  • type checked

Does it perform badly or incur higher memory overheads? Any reason to use arrays anymore?

like image 733
Robin Rodricks Avatar asked Nov 13 '10 01:11

Robin Rodricks


4 Answers

Plenty of reason. Vectors are not sparsely populated, for example, so that if your Vector has an index of 999, you have an array of 1,000 elements. In a standard Array, you could have as few as one.

like image 125
Robusto Avatar answered Nov 05 '22 00:11

Robusto


I'm answering my own question based on what I learnt of the issue.

Vectors are faster for these datatypes ONLY: -- (proof)

  • int
  • uint
  • Number
  • Boolean

Arrays are preferable for all other types:

  • Strings
  • classes

Vectors are more limited to work with:

  • reading/writing is bounds checked
    • non-existent slots cannot be set directly with [5] = Val
    • non-existent slots cannot be read (or you'll get an Exception)
  • must push() to create slots
  • sorting is slower

Vectors are more troublesome:

  • multi-D arrays cannot be convert to a vector
  • vector cannot be converted to a multi-D array
  • cannot work with String.split()
  • fixed type for all elements - unusable for JSON

Vectors can be easier to debug:

  • setting a [Vector of ints] into a [Vector of Vector of ints] will throw an Exception
  • compile time errors (in some cases)
    • when trying to set a [Vector of ints] into a [Vector of Strings]
    • when trying to set a [Vector of ints] into a [Vector of Vector of ints]
like image 30
Robin Rodricks Avatar answered Nov 04 '22 23:11

Robin Rodricks


Maybe specially when you don't know the type - it's a loosely based list, in a way. Like, say, with JSON data.

Edit: oh, here's another semi-reason - String.split(). That'll return you an Array of strings. Dunno why you can't get a Vector.<String> out of it, grr.

With that said, in Flash 10+, 99% of the time you'll be using Vectors instead. All 'disadvantages' of Vectors are just for very specific (often rare) use cases.

like image 35
zeh Avatar answered Nov 04 '22 23:11

zeh


As Robusto already said, vectors are not sparsely populated. Although this might be bad for for size, it is very-very good for speed. So if you don't need very sparse populated structures (and that to implement that sparse thing also takes some extra space), you can just take advantage of the speed.

It is the typical trade-of: memory vs. speed :-)

like image 1
Mihai Nita Avatar answered Nov 04 '22 23:11

Mihai Nita