Although the ordering of elements in the in.in loop is not guaranteed, what deviation between implementations exists in practice?

I don’t remember where, but once I noticed that this means that for..in loops can go through the elements in any order that can be implemented by the developers, including forward, backward, randomly or alternating back and forth for each execution a for..in . In practice, however, for some reason I do not think that the latter really takes place with any implementation. (Although, there is a certain browser that we know that likes to ruin everything, so you can never be too sure, but I'm distracted.)

My point is that, although in practice the deviation in the for..in sequence is probably not that bad, I would like to know what deviation, if any, exists between ECMAScript implementations. I believe that the main ones will be JScript, Chakra, Futhark, Carakan, JavascriptCore, SquirrelFish, V8, SpiderMonkey and TraceMonkey, for reference only.

+4
source share
1 answer

First of all, here is a link to the specification regarding the listing order:

12.6.4 in-in statement

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

The only ones I can think of are:

  • Earlier (perhaps current?) Versions of IE placed new properties that were added during the enumeration at the end of the enumeration so that they were visited by the current for-in run

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

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

    Testing in Firefox and V8 shows a different order.

This is not a big list. Of course, maybe more. I think this is summarized by @Pointy's comment. There is no guarantee. Even if you have an object that is reliably listed in a consistent way, this does not mean that it will do this the next time the version is updated. Use tools for how they are indicated for work, and not for what just works.

+5
source

Source: https://habr.com/ru/post/1446760/


All Articles