iteration order for Object

Brendan Eich brendan at mozilla.com
Mon Mar 14 07:21:05 PDT 2011


On Mar 14, 2011, at 5:09 AM, Lasse Reichstein wrote:

> On Fri, 11 Mar 2011 16:48:04 +0100, Charles Kendrick <charles at isomorphic.com> wrote:
> 
>> Just connecting the dots - I addressed this in my first email on the subject.  While it superficially sounds like a good compromise, I actually think it's the worst possibility: it requires browser vendors to implement limited order preservation, preventing deeper optimizations like sorted keys.
> 
> Implementors already have to do that. In V8, some objects have their
> properties backed by a hashmap. This just means that it needs an extra field
> in that hashmap (a 50% increase in size) to store the insertion index, and then
> extract and sort the keys by insertion index before iterating over them.

How do you get a 50% figure? I've studied V8 and JavaScriptCore as well as implemented lots of SpiderMonkey. Properties (however shared) need id, attributes, and value or offset. That's at least three words on practical architectures. What am I missing?


> That also means that some ideas that might work well in one implementation,
> like:
>  function hasKey(object) { for(var _ in object) return true; return false; }
> (which I believe works quickly in SpiderMonkey) will work much worse in other
> implementations - by at least a factor of 10.

Can you show a micro-benchmark with some data?


> So yes, (some) objects are hashmaps ... plus some more.
> 
> /L 'firmly in the "they are not ordered and never were, damnit!" camp'

But you just wrote a whole message about how objects have to expend space to preserve insertion order for enumeration! Saying you wish something weren't the case != saying it's not the case. :-P

Web developers find and exploit many de-facto standards. Enumeration order being insertion order for non-arrays at least, if not for all objects (arrays tend to be populated in index order), is old as the hills and web content depends on it, as far as I can tell. I'll do some experiments to try to get data on this.

/be



More information about the es-discuss mailing list