However it doesn't necessarily cause a large slowdown, just makes your function polymorphic and the resulting optimized code larger and a bit slower.
To avoid this entirely I recommend using a JavaScript `class` when passing objects to a function that has to run at peak speed.
function vec3 (x, y, z) { return {x, y, z} }I think they added this with ES2015.
They could change the representation to not be insertion order dependent, and store insertion order in a separate data structure, but that has its own trade-offs.
The "new" generation of JS VMs (V8, Chakra, Carakan) all dropped insertion order for array index properties (that is properties whose name is a uint32), but kept it for everything else; that broke about as much as browsers are willing to break, and breaking the general case would be far, far worse.
I mean this not as a slight on the ecmascript creators/maintainers but rather as an observation of how difficult the problem is.
It's complicated. The spec says order of for..in is not defined, browsers all implement the same order, and there keep being attempts to get the spec changed to match.
Object.keys, however _does_ have a defined order. https://tc39.github.io/ecma262/#sec-object.keys calls https://tc39.github.io/ecma262/#sec-enumerableownpropertynam... which calls the [[OwnPropertyKeys]] internal method. For normal objects this is defined at https://tc39.github.io/ecma262/#sec-ordinary-object-internal... which calls https://tc39.github.io/ecma262/#sec-ordinaryownpropertykeys which completely specifies the order: integer indices in ascending numeric order, then other string-named props in order of property creation, then symbol-named props.
This is pretty blatantly incorrect. Just because a problem is undecidable in general doesn't mean that there aren't specific cases where it can be solved. Optimizing compilers deal with undecidable problems all over the place, generally by either being conservative and lumping together proved-impossible with unable-to-be-proved invariants, or doing speculative optimizations with an unoptimized fallback. Just proving the type of a variable in Javascript is undecidable in the general case
My hypothesis (as someone who used to work on a JS VM) would be that locations of iterations over an object's keys and the creation of the object are almost always in different functions, with the object passed between them. And note that JS VMs rarely do cross-function optimization. As such, it would be exceptionally rare for the optimization to be applied.
My point was really that you can't always drop the ordering of properties, and the insertion order isn't something you can recreate after the time if you don't store in initially, hence you do really need to store it somehow.
(If anyone's confused, I edited the grandparent comment of this to better reflect the above.)
but how do you know it's wrong (if the iteration order of a .keys() call isnt the same)?
But a much more common case is that properties added after object creating will be added in an inconsistent order, for example:
o = {}; o.x = 1; o.y = 2; o.z = 3;
p = {}; p.y = 4; p.x = 5; p.z = 6;
When executing p.y = 4 you don't know that the object will eventually evolve to have the same properties as o.
Unless you forbid any impure operations you can't just deoptimise and re-execute code to then store the ordering. (I'm sure this isn't what you meant, and I'm probably being silly!)
https://richardartoul.github.io/jekyll/update/2015/04/26/hid...