A factory function will also give the objects it returns the same hidden class, resulting in monomorphic code that can be optimized.
function vec3 (x, y, z) { return {x, y, z} }Why does it have to be that the order of members matters? shouldn't both {x,y,z} and {z,x,y} compile down to {x,y,z}?
In JS for-in loop enumerates keys in the insert order. So {x,y,z} is different from {z,x,y}
AFAIK, that's not true; the spec doesn't guarantee that it iterates in insertion order, it's just how it's commonly implemented in browsers.
Nope, they should be iterated in their defined order: https://tc39.github.io/ecma262/#sec-ordinaryownpropertykeys
I think they added this with ES2015.
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.