JavaScript Object property ordering in javascript object property order javascript,object property order,object property order

I am trying to understand what's the thing with javascript Objects while using them as an associative array.

From ECMA:

4.3.3 An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

Using them in browser (chrome):

x = { 2: 'a', 3: 'b', 1: 'c' }
> Object {1: "c", 2: "a", 3: "b"}

y = { 'b': 2, 'c': 3, 'a': 1 }
> Object {b: 2, c: 3, a: 1}

While in the first example with the numbers as keys, they became ordered, in the second example with strings, they won't ( ordered = a,b,c ).

I am using these objects with string keys and I really don't want them to change order in some stage of app(if that's even possible) because it may crash the pipeline I am using.

Question is, is this approach safe and normal for every javascript machine, or should I use other method to guarantee that order won't ever change?

Thank you!

Edit: I am using this with node.js which runs on V8 (chrome engine), which 'orders non-numerical properties in insertion order'(Felix Kling). May this behaviour of V8 change?

Answer:1

Although Chrome may guarantee property order when using numbers as indexes in objects, the ECMA specification does not say it should do that, so by guarantee I'd not rely on this behavior. I suggest you to restructure your data to use arrays when you want to keep data order.

Answer:2

At the end of the day, it's because V8 uses if/else everywhere in the code to determine if it's an array index or an object property and not two separate classes to handle Array and Object separately like other JS engines:

https://lastzero.net/2009/09/object-property-ordering-in-google-chrome/

Yes, Google sticks to the specs, but it also makes it difficult to pass existing data structures from other programming languages to JS, because the ordering changes unexpectedly (even John Resig was surprised!). So you have to use different (possibly slower) data structures or use a converter, which makes code more complex.

Answer:3

I read from the spec.: Arrays are ordered, Objects not. Chrome seems to make no difference between number and string of numbers. (for performance reason?) I sometimes sidestep to an additional array of keys where needed.

Answer:4

Given the following file app.js $stateProvider .state('intro', { url: '/intro', templateUrl: 'partials/intro.html', controller: 'IntroController as IntroCtrl' }) ...

Given the following file app.js $stateProvider .state('intro', { url: '/intro', templateUrl: 'partials/intro.html', controller: 'IntroController as IntroCtrl' }) ...

My javascript foo is okay but I haven't kept up with the latest and greatest (we don't use Node.js in backend). What is the modern practice of structuring client side javascript? We organize ...

My javascript foo is okay but I haven't kept up with the latest and greatest (we don't use Node.js in backend). What is the modern practice of structuring client side javascript? We organize ...

This is my collection: { "_id" : 10926400, "votes": 131, "author": "Jesse", "comments" : [ { "id" : 1, ...

This is my collection: { "_id" : 10926400, "votes": 131, "author": "Jesse", "comments" : [ { "id" : 1, ...

I've just figured out that object in React's state that have multiple children cannot be rendered easily. In my example I have component which speaks with third-party API through AJAX: var Component ...

I've just figured out that object in React's state that have multiple children cannot be rendered easily. In my example I have component which speaks with third-party API through AJAX: var Component ...

  1. react deep object comparison
  2. react deep object compare
  3. react object deep copy
  4. react object deep merge
  5. react setstate deep object
  6. react deep clone object
  7. react state deep object
  8. react deep clone state object
  9. react js deep copy object