iteration order for Object

Charles Kendrick charles at isomorphic.com
Thu Mar 10 19:15:56 PST 2011


On 3/10/2011 6:50 PM, John Tamplin wrote:
> If you care about order, you don't use a hash map, and a JS object literal seems more closely
> related to a hash map than anything else.

Object behaved like a LinkedHashMap for 15 years, and still does behave like a LinkedHashMap, 
even in Chrome, except for numeric keys.

It seems like a very broadly held perception that Objects behave this way (based on the 
record-setting 127 stars on Chrome's issue for this, if nothing else).

> An alternative you didn't consider in your original post is using a single array, which is
> guaranteed to not change the order and never pick up additional properties:
>
> selectControl.setOptions([
> "storedValue1", "displayValue1",
> "storedValue2", "displayValue2",
> "storedValue3", "displayValue3"
> ])

I'm aware, but omitted it because I thought it was an even worse option.

It has all the allocation / GC drawbacks of the other approaches mentioned: two Strings per 
property vs just slots.  It also retains the drawback that developers have to build a secondary 
index to avoid O(n) property change costs, and that this will be slower in practice, for real 
applications.

On top of this, and perhaps worst of all, it has the further disadvantage that it looks like a 
list of values.  You can't look at the code and see that values are being mapped to one another.


More information about the es-discuss mailing list