I need to loop through a JavaScript object treating it as an array with custom keys. I know this is not fully supported, since properties have no instrinsic order, but since I always reorder the properties, I found this approach simple and reliable... until now.
The problem occurs when the keys are numbers, or strings that can be casted as numbers.
When I run this code:
var test1 = {4294966222:"A",4294966333:"A",4294966111:"A"};
var test2 = {4294968222:"A",4294968333:"A",4294968111:"A"};
for (var k in test1) {console.log(k);}
console.log("---");
for (var k in test2) {console.log(k);}
the output is:
4294966111
4294966222
4294966333
---
4294968222
4294968333
4294968111
Which means:
- (test1) if the keys are below 2^32 (4,294,967,296), they are automatically reordered, the smallest first
- (test2) if the keys are above 2^32, they are NOT reordered.
The question is: why is this happening?
Since all the browsers I tested (Google Chrome 79.0, Mozilla Firefox 71.0, Microsoft Edge 44.18362, Internet Explorer 11.535) agree about this output, there must be some official specification.
Update
I tested a lot of numbers before finding out it was a threshold matter. I found odd that the sequence 2,3,1 behaves differently from three timestamps ordered in the same way.