Recently I was wondering about different between using Object and Dictionary. One of the biggest pros about using Dictionary is possibility to use weak reference in key. This mechanism do not create an obstacles during Garbage Collection. Important fact is that weak references is quite expensive for collecting process, there is impact on overhead.
I have crated some test to show differences between Objects and Dictionaries.
Setup
compiler: AIR SDK 4.0
compiler mode: Release
flash player standalone: 11.9 Debug
sources: github
Each test was repeated 10 times and after that average time has been computed.
Test memory consuming
Objects
– cost of instance: 40B
– cost of key: 16B
Dictionary
– cost of instance: 56B
– cost of key: 16B
Tests Performance
Create instaces
Powered by TSBA.mobi GoogleGraph Wordpress plugin
Put values
Powered by TSBA.mobi GoogleGraph Wordpress plugin
Powered by TSBA.mobi GoogleGraph Wordpress plugin
Get values
Powered by TSBA.mobi GoogleGraph Wordpress plugin
Powered by TSBA.mobi GoogleGraph Wordpress plugin
Delete keys
Powered by TSBA.mobi GoogleGraph Wordpress plugin
Powered by TSBA.mobi GoogleGraph Wordpress plugin
Delete keys is most expensive operation on Hashtables. I recommend to use assign null instead removing keys (if this is possible) and then check that value over key is not null. From time to time it should be more optimal to make a cleanup and create new – clean hashtable.
Loop: for in
Powered by TSBA.mobi GoogleGraph Wordpress plugin
Loop: for each
Powered by TSBA.mobi GoogleGraph Wordpress plugin
This element is interesting, for each statement is 6 times faster that for in. But sometimes key is very important. My advice is that to save key inside value object.
Summary
- avoid to delete keys from Dictionary and Objects
- Dictionary is more memory consuming
- Dictionary with weak reference increment Garbage Collector overhead
- There is no big difference between putting and getting values between Dictionary and Object
- It is faster to create Objects by {}
- for each loop is faster than for in
Lol.
Please make a test with string keys for the Object and object keys for the Dictionary. That is what for they are.
Hi.
Agree, but this test is not about this. If there required to use simple object mapping like String<>Object then we gonna not use weekReference. In that case I prefer also to use Object as Hashmap.