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:
- 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 ofobj
sequentially) - meaning complexity of this operation for object with n keys isO(n)
; - 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?