On the official documentation of JSON
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
Note I am Javascript newbie, and from the name JSON (Javascript object notation) itself, I am assuming objects in Javascript are same as JSON. Please correct me in case I have got it wrong.
From the above definition it seems the Javascript objects are most probably implemented by either a hashmap or a BST or some similar data-structure.
But when I insert key value pairs in Node shell, they are inserted in serialised manner. This is what I tried in node shell
> var a = {}
undefined
> a['k1'] = 'a1'
'a1'
> a['k3'] = 'a3'
'a3'
> a['k2'] = 'a2'
'a2'
> a['k4'] = 'a4'
'a4'
> a['k5'] = 'a5'
'a5'
> a
{ k1: 'a1',
k3: 'a3',
k2: 'a2',
k4: 'a4',
k5: 'a5' }
Now, on printing a the key value pairs are returned in the same order as that of insertion. So my questions are:
Update Ok, so what about Javascript objects. Can someone comment on the underlying implementation of Javascript objects.
You're confusing JSON, which is only a text-based serialization format enabling simple data exchange, and plain javascript objects, which are unordered lists of properties.
As said by the MDN :
An object is a collection of properties, and a property is association between a name and a value. A value of property can be a function, which is then known as the object's method.
Objects properties can be seen as hash maps, as they're not ordered. But it's often a little more complicated : when objects are prototype based, properties not found on an object are searched upward the prototypes it's based on.
With javascript objects you get a guaranteed fast look-up, as this is an essential part of the implementation. But the implementation isn't defined by the norm and each engine can have its own.