Home  >  

Performance Tip: Dictionaries and Associative Arrays

Author photo
AddThis Social Bookmark Button
Here's a quick tip to help you squeeze extra performance out of your Flex/ActionScript applications when looping over and crunching lots of data on the client side. Use the dictionary class or associative arrays when you can!

Rather than looping through lots of data, trying to find the right object based on its properties, you can use associative arrays to find what you are looking for quickly and easily, without looping over anything. Associative arrays (also know as hashes or maps) allow you to create key value-pairs used to create a lookup table for data/objects in memory.

In ActionScript, you can use either a generic object class or an array to create a simple map using string based keys for your object values. Here's an example:

var map : Object = new Object();
map[ key ] = value; 
OR

var map : Array = new Array();
map[ key ] = value; 
When you want to access your object at runtime, you just need to provide the key to retrieve the hashed value:

mySavedValue = map[ key ]; 
The dictionary class is very similar to an associative array, however a bit more powerful. The dictionary allows you to create maps based on complex objects as the keys for the key-value pair. For example:

var map : Dictionary = new Dictionary();

var key1 : Object = new Object();
var key2 : Sprite = new Sprite();
var key3 : UIComponent = new UIComponent();

map[ key1 ] = value1
map[ key2 ] = value2
map[ key3 ] = value3
I've found this to be really helpful in scenarios where you may have had nested loops to process data. Rather than doing something like this:

for each ( var o1 : Object in myCollection1 )
{
    for each ( var o2 : Object in myCollection2 )
    {
        if ( o2.id == o1.relatedId )
        {
            //do something with the data that matches
            break;
         }
     }
}
You could do this, which would execute much faster:

var map : Object = new Object();

//first create a map
for each ( var o2 : Object in myCollection2 )
{
    map[ o2.id ] = o2;
}

//now, loop over the first collection and use the map
for each ( var o1 : Object in myCollection1 )
{   
    var foundObject : * = map[ o1.relatedId ];
    //now do something with the found object
}
Rather than looping over an unknown amount of items in 2 collections, you just loop over one collection and build the map, then loop over the other collection and access the properties of the map.

You might be surprised the difference in performance that this can make when used properly. You can use this for specialized label functions, looking up reference data, creating data maps, or just about any scenario where you would have to loop over lots of data. If you can get away with it, only build the map once, then access it any time you can throughout the application; this will ensure that you aren't wasting cpu looping through data when you don't need be.

When using the dictionary class, you also need to be careful and clean up after yourself. Otherwise you can end up with memory leaks. You can check out the livedocs for more information.
___________________________________
Andrew Trice
Principal Architect
Cynergy Systems
http://www.cynergysystems.com

Read more from Andrew Trice. Andrew Trice's Atom feed

Comments

2 Comments

Gareth said:

Nice! Finally a good, concise explanation of the Dictionary class. I was always a little confused as to what it actually was when I read other explanations, and how to implement it in a useful way.

One question though...should the "map" variable in the final example be of type "Dictionary" or will it still work as planned using a generic Object?

Gareth said:

Oops, never mind. I see that you are just comparing IDs in the final example, not complex objects.

Leave a comment


Type the characters you see in the picture above.

Tag Cloud

Poll: Sci-Fi Movies

What's Your Favorite Sci-Fi Movie of All Time?

Vote | View Poll Results | Read Related Blog Entry

Latest Features

  •     Welcome back to the series. This time we are goings to build a really exciting component that will be used to simply display information about the user. Well, you might say why to we need such a component, is there... Continue Reading
  •    Welcome back to our exciting Facebook ActionScript series. In this article we will discuss one of most important (and most exciting) features of the FB platform, it's the publishing of news. We all know when we log in to facebook,... Continue Reading
  • This article provides 10 tips and best practices (in no particular order) for maximizing the benefits that Dojo can bring to your next project. For a more thorough introduction to Dojo, see the article Dojo: The JavaScript Toolkit with... Continue Reading
  •     The notifications are one of the most interesting (and important) parts of the facebook area. In order to completely understand the Flash side of it, we need to understand the basics of the facebook notification, what it is and how... Continue Reading

Development Series

Get an overview of the tools and technologies that work together to allow developers to build Rich Internet Applications (RIAs) quickly and easily.

facebook icon Facebook Application Development

Anatomy of an Enterprise Flex RIA

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.