1

I am trying to make a test that shows that doing Object.keys(someObj) results in an undetermined order.

a = {}
a.b = 1
a.c = 2
a.d = 3
Object.keys(a)
// result should be undeterministic because Objects are not ordered

However no matter how I do it, the result is always the ['b', 'c', 'd'].

How can I get some variance in my result? example ['d', 'c', 'b']

nanomosfet
  • 132
  • 11
  • 3
    This will help your understanding a lot: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order ... tldr order is almost always preserved in modern browsers *but* you should never assume it is ordered. – Chris Cousins Aug 28 '18 at 02:45
  • 1
    Possible duplicate of [Sort JavaScript object by key](https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – Vikasdeep Singh Aug 28 '18 at 02:48
  • 1
    "How can I get some variance in my result?": https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – Ram Aug 28 '18 at 02:54
  • Possible duplicate of [Does JavaScript Guarantee Object Property Order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – smac89 Aug 28 '18 at 03:00
  • @ChrisCousins Exactly. There is a library that generates CSV headers by using the Object keys but I want to make a change to it so I can know 100% that the order will be correct. I cannot assume that the order will be correct if it is coming from the Object – nanomosfet Aug 28 '18 at 03:24
  • Then I would recommend editing the title of your question to "How do I ensure the order of the keys in an Object remains constant?" – Chris Cousins Aug 28 '18 at 03:28

3 Answers3

2

How can I get some variance in my result

Install and use an old version of a web browser.

All modern browsers preserve the insertion order; es6 Set and Map also preserve insertion order; You can use one of those to track your keys separately if you need to ensure this behavior.

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
  • "Install an old version of a web browser." Seriously? Is that a joke? – Ram Aug 28 '18 at 02:56
  • 3
    @undefined Why should it be a joke? It's a valid answer. – Brad Aug 28 '18 at 03:00
  • I'll admit it's funny, but it's not a joke. OP did not state why he wants variance. If he wants to prove a runtime returns undefined order, that's the only way short of randomizing the returned value. – Steven Spungin Aug 28 '18 at 03:00
1

Going through ECMA-262 you will start to find a lot of cases where what is in the spec does not match what engines actually do.

In most cases (such as this one) the engines still fulfill the specification. There is no way to prove that an engine isn't using non-deterministic ordering of property keys.

In other cases, engines may deviate from the spec, sometimes drastically.

I think the most important take-away here is that shouldn't really try to test non-deterministic behaviour*. The official test suite for ECMA-262 has some great examples of how to test all sorts of crazy behaviour. https://github.com/tc39/test262/tree/master/test/built-ins/Object/keys

*You can do fuzz-testing and such but that's way out of the topic here.

snek
  • 1,980
  • 1
  • 15
  • 29
0

Simple to see it using numeric keys which for some reason get numerically sorted when passed through Object.keys() in at least 2 browsers I ran this on

const o ={
   '3':3,
   '1':1,
   '2':2
}

console.log(Object.keys(o))// ['1','2','3']
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • There are different kinds of property keys in JavaScript. [`integer-indexed`](https://tc39.github.io/ecma262/#integer-index) keys are always ordered. – snek Aug 28 '18 at 03:23