<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/dictionaries-and-associative-a.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.23408-</id>
  <updated>2009-11-05T20:13:56Z</updated>
  <title>Comments for Performance Tip: Dictionaries and Associative Arrays (http://www.insideria.com/2008/04/dictionaries-and-associative-a.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23408</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/dictionaries-and-associative-a.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=23408" title="Performance Tip: Dictionaries and Associative Arrays" />
    <published>2008-04-16T01:35:45Z</published>
    <updated>2008-04-16T02:29:01Z</updated>
    <title>Performance Tip: Dictionaries and Associative Arrays</title>
    <summary>Here&apos;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!</summary>
    <author>
      <name>Andrew Trice</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[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!
<br/><br/>
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.  <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=10_Lists_of_data_4.html" target="_blank">Associative arrays</a> (also know as hashes or maps) allow you to create key value-pairs used to create a lookup table for data/objects in memory.  
<br/><br/>
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:
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">var</span> map : <span class="category2">Object</span> = <span class="category1">new</span> <span class="category2">Object</span>();
map[ key ] = value; </pre>
</code>

</div></div> 

OR
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">var</span> map : <span class="category2">Array</span> = <span class="category1">new</span> <span class="category2">Array</span>();
map[ key ] = value; </pre>
</code>

</div></div> 

When you want to access your object at runtime, you just need to provide the key to retrieve the hashed value:
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
mySavedValue = map[ key ]; </pre>
</code>

</div></div> 

The <a href="http://livedocs.adobe.com/flex/3/langref/flash/utils/Dictionary.html" target="_blank">dictionary</a> 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:
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">var</span> map : Dictionary = <span class="category1">new</span> Dictionary();

<span class="category1">var</span> key1 : <span class="category2">Object</span> = <span class="category1">new</span> <span class="category2">Object</span>();
<span class="category1">var</span> key2 : Sprite = <span class="category1">new</span> Sprite();
<span class="category1">var</span> key3 : UIComponent = <span class="category1">new</span> UIComponent();

map[ key1 ] = value1
map[ key2 ] = value2
map[ key3 ] = value3</pre>
</code>

</div></div> 

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:
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">for</span> each ( <span class="category1">var</span> o1 : <span class="category2">Object</span> <span class="category1">in</span> myCollection1 )
{
    <span class="category1">for</span> each ( <span class="category1">var</span> o2 : <span class="category2">Object</span> <span class="category1">in</span> myCollection2 )
    {
        <span class="category1">if</span> ( o2.id == o1.relatedId )
        {
            <span class="linecomment">//do something with the data that matches</span>
            <span class="category1">break</span>;
         }
     }
}</pre>
</code>

</div></div> 

You could do this, which would execute much faster:
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">var</span> map : <span class="category2">Object</span> = <span class="category1">new</span> <span class="category2">Object</span>();

<span class="linecomment">//first create a map</span>
<span class="category1">for</span> each ( <span class="category1">var</span> o2 : <span class="category2">Object</span> <span class="category1">in</span> myCollection2 )
{
    map[ o2.id ] = o2;
}

<span class="linecomment">//now, loop over the first collection and use the map</span>
<span class="category1">for</span> each ( <span class="category1">var</span> o1 : <span class="category2">Object</span> <span class="category1">in</span> myCollection1 )
{   
    <span class="category1">var</span> foundObject : * = map[ o1.relatedId ];
    <span class="linecomment">//now do something with the found object</span>
}</pre>
</code>

</div></div> 

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.    <br/><br/>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.
<br/><br/>
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 <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=10_Lists_of_data_4.html" target="_blank">livedocs</a> for more information.<br/>
___________________________________<br/>
<strong>Andrew Trice</strong><br/>
Principal Architect<br/>
<a href="http://www.cynergysystems.com" target="_blank">Cynergy Systems<br/>
http://www.cynergysystems.com</a>
]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23408-comment:2016614</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23408" type="text/html" href="http://www.insideria.com/2008/04/dictionaries-and-associative-a.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/dictionaries-and-associative-a.html#comment-2016614" />
    <title>Comment from Gareth on 2008-04-16</title>
    <author>
        <name>Gareth</name>
        <uri>http://flexfusion.archfamily.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://flexfusion.archfamily.com">
        <![CDATA[<p>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.</p>

<p>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?</p>]]>
    </content>
    <published>2008-04-16T15:16:58Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23408-comment:2016616</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23408" type="text/html" href="http://www.insideria.com/2008/04/dictionaries-and-associative-a.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/dictionaries-and-associative-a.html#comment-2016616" />
    <title>Comment from Gareth on 2008-04-16</title>
    <author>
        <name>Gareth</name>
        <uri>http://flexfusion.archfamily.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://flexfusion.archfamily.com">
        <![CDATA[<p>Oops, never mind.  I see that you are just comparing IDs in the final example, not complex objects.</p>]]>
    </content>
    <published>2008-04-16T15:22:50Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23408-comment:2119557</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23408" type="text/html" href="http://www.insideria.com/2008/04/dictionaries-and-associative-a.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/04/dictionaries-and-associative-a.html#comment-2119557" />
    <title>Comment from Ramian D on 2009-09-27</title>
    <author>
        <name>Ramian D</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>I just read elad elrom's article about hashmap, and i would disagree with you on that associative arrays doesn't iterate on lookup:</p>

<p><a href="http://elromdesign.com/blog/2008/03/27/boost-memory-performance-on-data-structures-using-hashmapcollection-api/">http://elromdesign.com/blog/2008/03/27/boost-memory-performance-on-data-structures-using-hashmapcollection-api/</a></p>

<p>if i misunderstood, please correct me..</p>]]>
    </content>
    <published>2009-09-28T06:01:03Z</published>
  </entry>

</feed
