typed-objects-explainer icon indicating copy to clipboard operation
typed-objects-explainer copied to clipboard

Memory order?

Open CoDEmanX opened this issue 10 years ago • 4 comments

The given example var PointType = new StructType({x: float64, y: float64}) uses an object to specify which types are supposed to be used and what their attribute keys are. But object keys are unordered in JS, so it's unclear in which order they are "[...] laid out in memory consecutively"[1]. Or am I overlooking something here?

The order will usually not matter, but it may in serialization.

CoDEmanX avatar Dec 11 '15 14:12 CoDEmanX

@CoDEmanX the order IS guaranteed for object literals, iirc.

nikomatsakis avatar Dec 11 '15 15:12 nikomatsakis

Was just checking with @dherman. He clarified that the intention of ES6 was to fully specify the order in those cases, but that it was something we ought to revisit to see what precisely wound up in the spec and make sure that it is guaranteed, or at least guaranteed enough.

nikomatsakis avatar Dec 11 '15 15:12 nikomatsakis

Maps do preserve insertion order: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Map

It's not quite clear for objects. The ECMA-262 v3.0 specs explicitly stated, that the order is not guaranteed (4.3.3). In v5.1 this sentence is gone, but there's still a note on the unordered nature found under for-in (12.6.4). They removed it in v6.0, but under Object.keys() you can find (19.1.2.14):

If an implementation defines a specific order of enumeration for the for-in statement, the same order must be used for the elements of the array returned in step 4.

The "if" is quite irritating.

CoDEmanX avatar Dec 11 '15 16:12 CoDEmanX

This is why the property names / types should probably be specified via an array if users want explicit ordering, e.g.

var PointType = new StructType([['x', float64], ['y', float64]]);

If the user does not specify the order, it should be up to the engine to order them in the most space efficient way - e.g. for alignment reasons you probably don't want an object to have a layout like:

int16, int32, int16

when you can have:

int16, int16, int32

phpnode avatar Dec 11 '15 17:12 phpnode