1

Let's say we have the following object:

const obj = {
  a: 1,
  b: 2,
  c: 4,
  d: 13
};

And then we try to access a property of it:

obj.d

How is this done? I can imagine 2 ways:

  1. traversal of all key-value pairs each time we try to get value of object (meaning that if we would do obj.d, we would traverse all previous keys of obj sequentially) - meaning complexity of this operation for object with n keys is O(n);
  2. some axillary tree-like structure is there for each object, so getting value for some key is done faster then with O(n)

There might be some optimizations, like storing all keys in some order, so we can live without axillary structure. But questions still holds: How JavaScript resolves value of object property?

2
  • 1
    The spec does not stipulate any particular implementation, but generally it's something like a hashing scheme that's heavily optimized. Commented Feb 18, 2021 at 16:05
  • 1
    I don't believe this is specified. It depends on each implementation. Commented Feb 18, 2021 at 16:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.