27
Mar
08

Increase memory performance on data structures using HashMapCollection API

Hash map is a data structure that can associates keys with values. It provides constant time performance for the basic operations of addItem, removeItemAt, getItemValue and other methods. The main reason for the increase in memory performance is iteration. Unlike ArrayCollection, the HashMapCollection doesn’t need to loop through in order to find a key or a value. The HashMapCollection associates a value with a key.HashMap Collection is a great choice when you need to:

  1. When you need in-memory data structures.
  2. When you need a pair of key-value data structure.
  3. When you need to Increase performance.

Flex and Action Script provides the “IList” interface that is implemented by the Array and ArrayCollection. Using array is a great choice when you need to associate an object with an ID, but if you have scenarios where you want to copy a large database structure to the memory or have a key-values pairs you will have to iteration through the array in order to do a lookup.
The API I developed is called “HashMapCollection” and it is using naming conventions that are very similar to the “ListCollectionView”.

The API is also implementing the change events classes, so you will be able to start using the API right away with much similarity to the “IList” methods.

Let’s take an example. Say we have list of contacts and phone numbers and we need to store them in the memory, the list was given to us through the SQL database and it has about 10,000 contacts.

Action Scriot HashMap Collection example

Using Array or ArrayCollection will force us to loop through the list until we find the name we are looking for and then get the phone number, that process will put a large expense on Flash player and will basically look as if the program is stuck, since action script is a single thread programming language, which means that the program cannot split itself into more than one simultaneously tasks.

The HashMapCollection doesn’t need to loop it’s basically using the following code to find the value:

map[key] = value;

I am giving a list of contacts, as an example, but let me make it clear that the HashMapCollection is flexible to use any type of key-value such as;

• Name-UIComponents
• Name-Object
• Name-Array

Below you can find a use-case example of creating a data structure and modifying the data;


private function map():void {

var map:IMap = new HashMapCollection();
map.addEventListener(CollectionEvent.COLLECTION_CHANGE, handler);

map.addItem(”John”, “212-452-8086″);
map.addItem(”James”, “718-345-3455″);
map.addItem(”Micheal”, “917-782-8822″);
map.addItem(”Ron”, “212-426-8855″);
map.addItem(”Mike”, “212-255-2436″);
map.addItem(”Jenny”, “718-344-2433″);
map.addItem(”Jack”, “917-222-4352″);
map.addItem(”Riki”, “981-222-1122″);
trace(”\nAll items: “+map.toString()+”\n”);

trace(”containsKey Jack? “+map.containsKey(”Jack”));
trace(”containsValue 718-344-2433? “+map.containsValue(”718-344-2433″));
trace(”getItemKey 718-344-2433: “+map.getItemKey(”718-344-2433″));
trace(”getItemValue Jenny: “+map.getItemValue(”Jenny”));

map.removeItemAt(”Riki”);
trace(”Remove Riki.”);
trace(”getItemValue Riki: “+map.getItemValue(”Riki”));
trace(”Comapre: “+map.compare(”Ron”, “212-426-8855″));

map.removeAll();
trace(”\nAll items: “+map.toString()+”\n”);

}

private function handler(event:CollectionEvent):void
{
trace(”Event: “+event.kind);
}

All of this is great, but let me actually prove to you that you gain performance. I created a small Flex application that creates two data structures; ArrayCollection and HashMapCollection. Watch the memory monitor and see how Flash Player actaully stopped while it loops and looks for the key in the ArrayCollection. On the other hand the HashMap doesn’t add any expense on the Flash player.

All source code contained in this API has been published under the MIT licence and is protected as stated therein.

To see the example and download source code (via right click):
http://elromdesign.com/blog/Flex/API/HashMapCollection/

To view the ASDOC of all the API:
http://elromdesign.com/blog/Flex/API/asdoc/

Have fun!


7 Responses to “Increase memory performance on data structures using HashMapCollection API”


  1. 1 Dr.P Jan 25th, 2010 at 3:36 pm

    This is great. But I have one question. I’m assuming the HashMapCollection cannot be used for a dataProvider.

  2. 2 elad.ny Jan 25th, 2010 at 4:25 pm

    You can create a custom component to handle the hashmap or create a helper to take the data and massage it so it fit the exacted format as data provider.

  3. 3 Dr.P Jan 26th, 2010 at 9:28 am

    Right. This would include also other functionality that the dataProvider needs such as Filter and Sort. Thanks.

  4. 4 elad.ny Jan 26th, 2010 at 9:46 am

    It would be nice if Adobe rolls out an out of the box implementation of HashMap that allow attaching it to Flex components such as DataGrid, meanwhile this is what we got :)

  5. 5 Dr.P Jan 26th, 2010 at 2:51 pm

    Yeah, it would be super nice. Unfortunately I’m a noob at Flex so I’ll have to see what I can do cause I’d really like to use this with the dataProvider. Anyways, thanks for the info.

  6. 6 Hussein Apr 22nd, 2010 at 11:21 pm

    Love it! Thanks a million. Great work.

  7. 7 Xavier Aug 19th, 2010 at 8:34 am

    Hey Elad!,
    this looks really usefull for the “lookups” but what do you think about the creation of each element and the allocated memory for the whole HashCollection? I mean, if you have to create 2 000 000 elements for the Hash-Collection, you would also have to wait until its done or not? How is the impact adding an extra “[]” for each element?
    thankx, great example.