While element ordering in for..in loops isn't guaranteed, what deviation between implementations is there in practice?

enumeration, for-in-loop, for-loop, javascript, object

Solution

First of all, here's a reference from the specification with regard to the order of enumeration:

12.6.4 The for-in Statement

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified. Properties of the object being enumerated may be deleted during enumeration. If a property that has not yet been visited during enumeration is deleted, then it will not be visited. If new properties are added to the object being enumerated during enumeration, the newly added properties are not guaranteed to be visited in the active enumeration. A property name must not be visited more than once in any enumeration.

The only ones I can think of off hand are these:

Earlier (maybe current?) versions of IE would place new properties that were added during enumeration to the end of the enumeration so that they would be visited by the current running `for-in`

V8 has a different order of enumeration as described in this bug report, which was closed as WorkingAsIntended

 var a = {"foo":"bar", "3": "3", "2":"2", "1":"1"};

Testing in Firefox and V8 show a different order.

This is not much of a list. There certainly may be more. I think it is summed up by @Pointy's comment. There's no guarantee. Even if you have an object that reliably enumerates in a consistent manner, this doesn't mean that it will do so with the next version upgrade. Use tools for how they're specified to work, not for what merely seems to work.

Problem

I don't remember where, but I once saw it put that `for..in` loops can go through the elements in any order the implementors like, including forward, backward, randomly, or alternating between forward and back for each execution of a `for..in` loop. In practice, though, somehow I don't think that the latter is really the case with any implementation in existence. (Although, there is a certain browser we know who likes to mess things up, so you can never be too sure, but I digress.) My point is that while there probably isn't such bad deviation in `for..in` sequencing in practice, I'd like to know what deviation, if any, there is between ECMAScript implementations. I suppose the main ones now would be JScript, Chakra, Futhark, Carakan, JavascriptCore, SquirrelFish, V8, SpiderMonkey, and TraceMonkey, just for reference.

Original source

Related problems