node.js - Nodejs Order of properties guarantee -


note: using nodejs may or may not have subtle differences vanilla ecmascript's standards.

i have heard when using for-each loop iterate on properties of object, should not count on properties being in same order. (even though in practice have never seen case objects iterated on in different order). in production, have believe typo object created overwritten property.

var obj = {   a: 'a property',   b: 'another property',   c: 'yet property',   a: 'woah have a?' } 

in nodejs, guaranteed second property containing string 'woah have a?' always shadow first property containing string 'a property'?

(even though in practice have never seen case objects iterated on in different order) following should give different order in v8 atleast.

var obj = {   "first":"first",   "2":"2",   "34":"34",   "1":"1",   "second":"second" }; (var in obj) { console.log(i); }; // order listed: // "1" // "2" // "34" // "first" // "second" 

as discussed here

ecma-262 not specify enumeration order. de facto standard match insertion order, v8 does, 1 exception:

v8 gives no guarantees on enumeration order array indices (i.e., property name can parsed 32-bit unsigned integer).

remembering insertion order array indices incur significant memory overhead.

though above says enumeration order not specified after object created. think can safely assume insertion order should remain consistent, there no point engine otherwise , alter insertion order.

var obj = {   "first":"first",   "2":"2",   "34":"34",   "1":"1",   "second":"second",   2: "two" }; // gives result { '1': '1',   '2': 'two',   '34': '34',   first: 'first',   second: 'second' } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -