iteration order for Object

Charles Kendrick charles at isomorphic.com
Fri Mar 11 07:43:56 PST 2011


On 3/11/2011 7:07 AM, Claus Reinke wrote:
>> I believe it is very very important that the ECMAScript standard specify that when a new
>> Object is created, for..in iteration traverses properties in the order they are added,
>> regardless of whether the properties are numeric or not.
>
> Some users might prefer 'in the order of keys'. That is predictable,
> and allows for efficient implementation (both sparse and dense).

This:

1. breaks completely with the de-facto standard.

2. still requires the verbose, high allocation cost data formats previously explained in order 
to define an in-order map

3. prevents the use of Object literals in JSON to convey ordered data

A SortedMap collection for JavaScript would be great, and useful for certain types of code. 
But it doesn't help with the problems I'm pointing out.

> Most of these are just awkward ways of saying "this
> is the order I want" and "I also want hashmap access". So why not write that out explicitly,
> with an optional ordering parameter, making the enumeration explicit
> when the default ordering isn't suitable:
>
> selectControl.setOptions({
> storedValue1 : "displayValue1",
> storedValue2 : "displayValue2",
> storedValue3 : "displayValue3"
> },['storedValue1','storedValue2','storedValue3'])

Because this is spectacularly bad in all the ways I previously mentioned: even more redundancy 
than the prior worst option, even more allocation and GC load.

> It may seem convenient if keys happen to be enumerated in the order they are written, but if
> that extends to the order of insertion, things get complicated (if you delete an entry, then
> reinsert it, has it lost "its" position? do you want an "insert before"?).

There are several clear behaviors to choose from here (eg Java's LinkedHashMap).


More information about the es-discuss mailing list