Member Ordering

Brendan Eich brendan at mozilla.org
Tue Jun 3 11:56:33 PDT 2008


On Jun 3, 2008, at 9:25 AM, Douglas Crockford wrote:

> ES3 says that objects are unordered with respect to the sequence  
> produced by
> for...in. Nevertheless, many web developers have an expectation  
> that the
> sequence should reproduce the insertion order. That implied order  
> is strictly
> complied with in all browsers except Opera which can optimize  
> numeric sequences.
>
> We like the Opera optimization. Web developers don't seem to notice  
> that it
> diverges from common practice, except that for in on arrays seems  
> to work
> correctly in cases that seem to be confused on other browsers.

Confused in other browsers *only* if one creates the array's indexed  
properties in other than index order. That's rare, so it's not a huge  
hardship and I hope we can do what you propose, even though it's an  
incompatible change from past releases of most browsers. From a  
Firefox 2's JavaScript 1.7, in the SpiderMonkey shell:

js> a = []

js> a[3]=3
3
js> a[2]=2
2
js> a[1]=1
1
js> a[0]=0
0
js> for(i in a)print(i)
3
2
1
0


> We are also reluctant to slap Opera for having produced the best  
> implementation
> of this feature in a way that fully complies with the current  
> standard.

You would not be slapping only Opera. Firefox 3's JavaScript 1.8  
matches it:

js> a = []
[]
js> a[3]=3
3
js> a[2]=2
2
js> a[1]=1
1
js> a[0]=0
0
js> for(i in a) print(i)
0
1
2
3

but only for dense arrays: length <= 32 or load factor >= .25 -- and  
no ad-hoc named, direct properties. I hear Opera has similar  
restrictions, but I haven't tested.

There are other hard cases:

js> a = []

js> a[3]=3
3
js> a[2]=2
2
js> Array.prototype[1] = 1
1
js> Object.prototype[0] = 0
0
js> for (i in a) print(i)
2
3
1
0


> We want to better specify the ordering of for...in because the  
> developer
> community has expectations. We are reluctant to impose a particular
> implementation of objects, but we do like that Opera's optimization  
> seems to
> best match expectations in a way that is the least surprising, and  
> possibly the
> best performing.
>
> How would the other three feel about having to adopt Opera's  
> convention?

We need an exact spec of what Opera does to agree, or better: we need  
a spec that matches Opera and Firefox 3 for the easy case of dense  
arrays with no prototype indexed properties and no ad-hoc named  
properties.

I'm in favor of for-in using index order for arrays, provided we can  
get the hard cases "right" for some reasonable definition of "right".  
We'd adapt future Mozilla releases to match.

/be



More information about the Es4-discuss mailing list