Code

Flash AS3 Showdown: Object vs. Dictionary

I decided to see what the difference was between using a Dictionary and an Object to store simple name:value pairs, where name was always a String. Fired up the Flash IDE and typed out the following code:

import flash.sampler.getSize;
import flash.utils.Dictionary;
var a:uint;
var s:Date;
var find:Array = new Array()
var val:*;

trace('creating 20000 entries');
s = new Date();
var d:Dictionary = new Dictionary();
for (a = 0;a<20000;a++) {
d[String('name'+a)] = a;
}
trace('Dictionary creation: '
+ (new Date().valueOf() - s.valueOf()) + 'ms'
+ ', size: ' + getSize(d) + ' bytes');
s = new Date();
var o:Object = {};
for (a = 0;a<20000;a++) {
o[String('name'+a)] = a;
}
trace('Object creation: '
+ (new Date().valueOf() - s.valueOf()) + 'ms'
+ ', size: ' + getSize(o) + ' bytes');

for (a = 1;a <1000;a++) {
find.push(20000/a);
}
trace('reading 1000 keys');

s = new Date();
for (a = 0;a<1000;a++) {
val = d['name' + find[a]];
}
trace('Dictionary read: '
+ (new Date().valueOf() - s.valueOf()) + 'ms');

s = new Date();
for (a = 0;a<1000;a++) {
val = o['name' + find[a]];
}
trace('Object read: '
+ (new Date().valueOf() - s.valueOf()) + 'ms');


trace('search for 1000 values');
s = new Date();
for (a = 0;a<1000;a++) {
for each(val in d) {
if (val == find[a]) break;
}
}
trace('Dictionary search: '
+ (new Date().valueOf() - s.valueOf()) + 'ms');

s = new Date();
for (a = 0;a<1000;a++) {
for each(val in o) {
if (val == find[a]) break;
}
}
trace('Object search: '
+ (new Date().valueOf() - s.valueOf()) + 'ms');

Which produced the following trace results:

creating 20000 entries
Dictionary creation: 47ms, size: 160032 bytes
Object creation: 47ms, size: 160024 bytes

So, same speed, and Object is a tiny bit smaller. In multiple runs this was almost always the same value - often enough to be a reliable value.

reading 1000 keys
Dictionary read: 16ms
Object read: 16ms

Again, for direct key reads, the two are same speed. In multiple runs this was always the same value.

search for 1000 values
Dictionary search: 2047ms
Object search: 2219ms

Now here, Object falls down bit. The interesting thing is that, despite having the same values created for the search each time, the search ms were all over the map. Each time I fired this, I got a different value. I'm assuming this much churn was causing the garbage collector to kick in sporadically. That said, the difference between the values would vary from 100 to 400 ms. The one constant though, was that Object searches were almost always slower than Dictionary searches, and the instances where Object was faster were few and far between. However, the time was always just over 2000ms for each. So, the average speed per search was still aprox. 2-2.5ms per search for both Object and Dictionary.

Conclusion: unless you really need to use an strict type checking for your keys, then there's no discernible difference between the two, except that using Object saves you an import.

Tags: , ,
2009.08.14 11:14 PM | Permalink 0 Comments