Performance Tip: Dictionaries and Associative Arrays

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值