So let's say I have something like the following in javascript:
object = {"one": 1, "two": 2, "three": 3, "123": 123};
When I iterate through this, it turns out that the "123" member has jumped to the front of the list, while the order of the others is maintained. If I do something like the following though,
object = {"one": 1, "two": 2, "three": 3, "123 STRING": 123};
the order is maintained. So it seems like a purely numeric key is bumped up, but a key containing non-numeric characters is not. Anyone know why?
In V8 the data-structures behind an object vary a lot:
Named properties are in insertion order because that's most natural with the hidden class evolution/transitions.
Indexed properties are in their actual value order because it would be a waste of memory to store the insertion order.
If you are backed by a hash table, the hash table deliberately fakes the same order that results from the natural optimizations.
It is the same in other browsers too because storing indexed properties, when the numbers are small, in an actual "C array" is a huge optimization. What can vary is whether indexed properties are listed first or named properties.
The spec made this optimization possible by not defining iteration order and V8 was first (at least according to that bug thread) to exploit it.
Indexed keys being listed before named and vice versa is of course arbitrary and doesn't affect any optimizations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With