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:
OR
When you want to access your object at runtime, you just need to provide the key to retrieve the hashed value:
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:
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:
You could do this, which would execute much faster:
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.
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;
var map : Array = new Array();
map[ key ] = value;
mySavedValue = map[ key ];
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
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;
}
}
}
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
}
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.