2

I have seen several questions, on Stack Overflow and in other places, asking how to implement an ordered hash, ordered object, or ordered dictionary in Javascript. Here are a few examples:

To me, it appears like according to the official ECMAScript spec, the order of the properties in an object is "implementation dependent", although most browsers loop over the properties in the order in which they were defined. I can imagine some programmers testing their scripts and seeing everything work fine, unaware that, it may behave in unexpected ways in a few situations, at least in principle.

My question is this: Could they change the spec to specify the order of the properties? To me, it doesn't seem like it would be too hard, but I don't know if I am missing something. This would encourage the makers of the few remaining browsers to do what everyone else does and add a useful feature. They would basically be saying that JavaScript interpreters should do what most JavaScript interpreters do anyway. I can't imagine a change like this causing any sort of compatibility issues.

If this would not be a realistic option, why not? I don't have any immediate practical goals. I am trying to satisfy my curiosity as to why the language is the way it is.

Community
  • 1
  • 1
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
  • 1
    I'm not sure what you're expecting as an answer. You could make a suggestion concerning the specs to Ecma International. "Would it be reasonable" is not really an appropriate question for Stack Overflow. – pimvdb Dec 14 '11 at 21:58
  • 1
    I thought about that. For some reason, I thought they may have had a good reason not to have already done that, and I was just curious about why. I am starting to consider actually contacting Ecma just to see what they would say. If I do that, I may post their response here. – Elias Zamaria Dec 14 '11 at 21:59
  • 2
    I'd vote *Yes* for that. I'm not sure the process for presenting ideas to the ECMA board. I used to have friends at Microsoft that went to the standards meetings, though I'm sure it was just for the free food since they sure as hell never implemented anything standard. – Mike Christensen Dec 14 '11 at 22:05
  • @mikez302: It was discussed for ECMAScript 5. Here's a sample email from the archives: https://mail.mozilla.org/pipermail/es-discuss/2010-December/012469.html – Tim Down Dec 14 '11 at 22:13

2 Answers2

3

Specifying the enumeration order of object properties was discussed for ECMAScript 5 but dropped because there was found to be no de facto standard across browsers, from what I can gather.

It's certainly a reasonable desire, particularly since the rise of JSON.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
2

Storing a hashtable costs you less memory than a hashtable with preserved order1. I guess this is one of the reasons why the spec allows one to ignore the order.

Major browsers are preserving the entries order probably not to break existing scripts written by unaware developers. However I can imagine EcmaScript implementation with minimal memory footprint that would sacrifice this non-standard feature.


1 E.g. in Java you have a choice between HashMap and LinkedHashMap.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674