Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are objects in JavaScript stored in memory?

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:

  • Do I get fast lookups for a key? I mean complexity of O(log(n)) or better.
  • In case JSON is not using a data-structure like a BST, hashmap or some similar DS, then how exactly are JSON objects layed under the memory?

Update Ok, so what about Javascript objects. Can someone comment on the underlying implementation of Javascript objects.

like image 346
Sushant Gupta Avatar asked Dec 29 '12 13:12

Sushant Gupta


People also ask

How does JavaScript store data in a data object?

Fortunately, JavaScript provides a data type specifically for storing sequences of values. It is called an array and is written as a list of values between square brackets, separated by commas. The notation for getting at the elements inside an array also uses square brackets.

Where does JavaScript store memory?

Memory Storage in Javascript # There are two places Javascript stores this data: The stack, which is a scratch space for the current Javascript thread. Since Javascript is usually only single threaded, there is usually one stack. The stack is also limited in size, which is why numbers in Javascript can only be so big.

How are JavaScript arrays stored in memory?

In Javascript, an array is a Hashtable Object type so the interpreter doesn't need to keep track of physical memory and changing the value of an element doesn't affect other elements as they're not stored in a contiguous block of memory.

How are memory allocated to variables in JavaScript?

In contrast, JavaScript automatically allocates memory when objects are created and frees it when they are not used anymore (garbage collection). This automaticity is a potential source of confusion: it can give developers the false impression that they don't need to worry about memory management.


1 Answers

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.

Update

Starting from ES6, which you have in almost all browsers and JS servers in 2021, the order of non integer keys in object is the insertion order.

like image 71
Denys Séguret Avatar answered Oct 01 '22 17:10

Denys Séguret