Hi all, I was testing something interesting fact... Maybe you are JS-Expert who likes to use Array a lot! I hope after reading my blog... You changed your coding to use less Array... Why? Because in my comparison test, Array is slower than Object in the case if you can substitute Array with Object. First at first, Please go to Test page to see the timing comparison (Careful, this may take 20 seconds to do, it will freeze your browser) between using Array and Object. In my computer, surprisingly it could cut your performance ranged from 30-50%... What a shock! Once again, try to organize such a way so that you dont ever need Array... This test is purposed for my rendering data for GFX 2.0 because I need to have fast writing and reading data for object rendering. Well, I am open type person, if you think what I stated is wrong or just chit chat for improving the speed then feel free to put comments in!
My GOD, Array is so slow... What should I do?
Submitted by robertusj on Fri, 05/16/2008 - 17:07.
- robertusj's blog
- Login or register to post comments
- Unsubscribe blog
- Unsubscribe post

A couple of things: First of
A couple of things:
First of all, it seems pretty fast in safari - I'm guessing you see slowness in IE.
Second of all, I have found that many times arrays (especially, I have found in IE) are faster if you do them backwards - rather than forwards....
That is, rather than doing something like:
{
//touch array
}
you could try something like:
while (i > 0)
{
i--;
//touch array
}
There are a few places online which describe this behavior - but I'm not sure of the reasoning behind how it actually works. Perhaps you can restructure your code a bit to access your arrays in that way...
arr....
Interesting!!! So basically you do something like
var i = max;
while (i > 0)
{
i--;
system.console(arr[i]);
}
I tried it and it is showing the same result just like we expected, array is slower than object...
I dont understand why array is slow than object... It suppose to be opposite way... Btw, I checked using Opera and FF... And both of them show that array is slower compared to object.
It depends on how you both build and access members in an array.
In general hashmaps will be faster (i.e. O(1)) than accessing members of an array; this is true of any language, not just Javascript.
That being said...I usually find it much more maintainable (aside from the performance gains from using a hashmap/dictionary/insert your choice of terminology here) to be using a property bag/hashtable/etc. Simply put, it makes it much easier for someone who did not author the code to understand the meaning of a set of values as opposed to having to guess it based on ordering, which leads to easier refactoring/maintenance in the long run.
arr
hi ttrenka,
What you said is absolutely right! Hashmap is faster than array and I am believing it (since I am computer scientist and the Big-Oh notation O(1) thingy very obvious) but it just does not right to use hashmap in this context application. Since in this context, I need a simple storage so that I can save items and browse them later on (even in sequence) whereas in hashmap kind of "selective" storage (give key and get something that mapped onto it). The problem is the O(1) thingy it has "hidden" calculation cost (I hate this one since make something not really accurate) so when you dont really make fully use of hashmap, it will make performance worse than you expected... I am pretty sure when you do comparison between array, hashmap and object... Object will be the fastest one! But if you say in different context where to be selective then I am absolutely agree on you! Btw I am interested to what kind of implementation you use for the Hashmap? I need it for my project so maybe if you dont mind to share the code so that I can save my time as well! Of course the credit of the implementation of HashMap will be yours! Hehe!
With JS, objects are hashmaps.
An object (i.e. new Object() or {}) in Javascript is a mutable property bag; there's nothing special within the language that's specifically a dictionary or a hashmap. We use the terms (Object vs. hashmap vs. property bag) interchangeably. What this means is that, in effect, *all* objects in JS are hashmaps. You can browse an object's fields/key-value pairs through the use of the for...in loop; the main difference is that the order in which you get those fields back is not guaranteed.
For examples, you can look all over the Dojo Toolkit codebase; any method that takes some sort of keyword argument object (think xhr*, or any Dijit created programmatically) is using an object as a hashmap.
One thing to keep in mind is that an Array in JS is (for all intents and purposes) actually an Object using integers as property keys.
WRT the hidden calculation costs with hashmap lookup...since this is such an inherent part of the language (all objects are mutable because they are deliberately designed on the hashmap concept), we expect (and study of the various language implementations should bear this out) that each implementation has taken the time to optimize, as best as possible, hash calculations for storage. You are correct in that there are hidden costs but given that it's such an integral part of the language, these are costs that we simply bear.
Ah! Object == Hashmap...
Hi ttrenka,
After I read your reply, I learn something new and what you said is actually true! I see the Object and if you said it is actually Hashmap and I believe it! Ah! I didnt know about that... Put aside that one... If I go back again to the problem of this topic whereas Object is faster than Array (and we believe that Array is actually Object with number as key) why it is slower than the Object itself? I just dont understand... If the difference like 0.1% it is fine... This one can be 10-30% performance difference...
Caveats
Just a reminder - there ain't no such thing as a free lunch! Generally performance and storage work in proportion: the more memory you throw at a problem, the better the performance and vice-versa. Hashmaps require extra storage for pointers that arrays don't need. And even though JS implementations are good at figuring out hashing functions, it cannot always choose perfectly. If you're unlucky enough to get a bad hashing function for your data set, the performance will be at least twice as bad as array lookup.
Hashmaps are particularly rough on your poor users with 64MB. But it also is potentially bad for single-page apps which stay at the same URL for hours. That causes lots of garbage collection, etc.
Craig Riecke, Dojo Committer
criecke@dojotoolkit.org