Objects and their internal representation in Javascript

--

The JavaScript standard allows developers to define objects in a very flexible way, and it is hard to come up with an efficient representation that works for everything. An object is essentially a collection of properties: basically key-value pairs. You can access properties using two different kinds of expressions:

obj.prop
obj[“prop”]
According to the spec, property names are always strings. If you use a name which is not a string, it is implicitly converted to a string. This may be a little surprising: if you use a number as a property name, it gets converted to a string as well (at least according to the spec). Because of this, you can store values at negative or fractional array indices. So a JavaScript object is basically a map from strings to values.

Internal Representation of an Object in Javascript

The below diagram quickly summarizes the internal representation of objects in javascript:

Internal Representation of an object

Most objects contain all their properties in a single block of memory (for example “a”, and “b” are in JSObject). All blocks of memory have a pointer to a map, which describes their structure. Named properties that don’t fit in an object are usually stored in an overflow array (like FixedArray that contains “c”, and “d”). Numbered properties are stored separately, usually in a contiguous array (in this case FixedArray having Property “0”, Property “1” and Property “2”).

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Vinay Chaturvedi
Vinay Chaturvedi

No responses yet

Write a response