I have this test:
let obj = {
"5555555555": 5,
"4444444444": 4,
"3333333333": 3,
"2222222222": 2,
};
console.log(Object.keys(obj));
The output is ["2222222222", "3333333333", "5555555555", "4444444444"]
However, the EcmaScript 2015 specification defines the order for [[OwnPropertyKeys]]:
- For each own property key P of O that is an integer index, in ascending numeric index order,
a. Add P as the last element of keys.
And integer index is defined as:
An integer index is a String-valued property key that is a canonical numeric String and whose numeric value is either
+0
or a positive integer ≤ 253−1. An array index is an integer index whose numeric value i is in the range +0 ≤ i < 232−1.
As all four properties in obj
, in the snippet above, are integer indexes by definition, how come they are not listed in numerical order when I call Object.keys
?
I see this same order in Chrome, Firefox, Edge, even Internet Explorer 11. It seems like they apply the "ascending numeric index order" for array indexes, not (all) integer indexes. Only that can explain why 1111111111 and 2222222222 are ordered first and in numerical order (the only two keys that are < 232-1), and 4444444444 and 5555555555 are ordered last in their original relative order.
What am I missing? Are these implementations violating the specs?