Populate a Flex’s ArrayCollection… a consideration about addItem() performance

I’m playing with Flex data Providers and actually I’m testing ArrayCollection. There are 3 ways to populate an Array collection:

  1. Initialize an ArrayCollection and then use its addItem() method (which accepts any Object type)
  2. Initialize an ArrayCollection, access its “embedded” Array by using the source property (ie: myArrayCollection.source) an then push objects inside this one
  3. Initialize an Array, populate it and then initialize an ArrayCollection by passing the Array to its constructor (ie: myArrayCollection:ArrayCollection = new ArrayCollection(myPreviousDefinedArray))

Last technique is (as far I saw) absolutely the fastest , I did a test in which I populated a collection with 300,000 objects and it taken about 9 seconds instead of 32 (350% slower) of first one! The second technique is also faster than the first, but the third is better.

So, what I learned is that addItem() should used only to add few items, instead for huge collection should be avoided in favor of the third technique described in order to obtain better performances.

  • PSmith

    I’m a newbie to flex and actionscript. Can you provide some code examples for the three ways of populating an arraycollection in actionscript?

  • Sure, I’m not a guru, but you can do the following:

    1) Initialize an ArrayCollection and then use addItem():

    var collection:ArrayCollection = new ArrayCollection();
    collection.addItem(MyItem);

    2) Initialize an ArrayCollection and then access its source (which is an array):

    var collection:ArrayCollection = new ArrayCollection();
    collection.source.push(MyItem2);

    3) Passing directly an array to the constructor:

    var collection:ArrayCollection = new ArrayCollection([“foo”, “foo2”, “foo3”]);

    This technique is the fastest ;)

  • R_Bob776

    Nice solutions (I just used your array-randomize routine), but I’ve a comment on ArrayCollection: whilst it’s nice and convenient, I’ve kind-of learned to do without it, largely because yer standard AS array is capable of storing other arrays, objects and hashes within itself. What’s the big advantage to that? Well, ActionScript offers list comprehension (check out the MAP function) for VERY quickly operating on every element of an array. This map() function has its roots in the Lisp world, and it’s a bit of a mind-bender the first time you do it, but with practice, you’ll come to favour it over loops*.

    Moreover, the more you stick to primitive lists, the more interoperability you get with AMF providers, and (let’s say) you end up implementing your server system in Python or Perl or Ruby (or even Lisp!), or something else with good functional programming implementations, you’ll be passing your server a list which it can begin to operate on natively, and vice versa. Even if you’ve no interest in server-side stuff, I promise you that your server programmer will appreciate you for it.

    *Caveat: if you’re writing a function which operates on a list in a order which you must determine, a loop is the way to go.

  • Paul

    This is a great tid-bit. Thanks for sharing.

  • It’s my understanding that ArrayCollection dispatches events for updates whereas Array does not. Can somebody correct me if I’m wrong?


  • Davide Zanotti:

    Sure, I’m not a guru, but you can do the following:
    1) Initialize an ArrayCollection and then use addItem():
    var collection:ArrayCollection = new ArrayCollection();
    collection.addItem(MyItem);
    2) Initialize an ArrayCollection and then access its source (which is an array):
    var collection:ArrayCollection = new ArrayCollection();
    collection.source.push(MyItem2);
    3) Passing directly an array to the constructor:
    var collection:ArrayCollection = new ArrayCollection([“foo”, “foo2”, “foo3”]);
    This technique is the fastest ;)

  • Great post, thats what I needed. ~350% performance gain :)