How expensive is it to access an object property of a large object?

Viewed 245

Background: I am a mostly self-taught programmer. I've been doing professional JS work for the last six years, but because of my lack of familiarity with computer science fundamentals, some things are not clear to me.

Considering the object:

const test = {a: 1, b: 2, c: 3}  

you could find the value of property b in two ways. Path 1:

console.log(test.b)  

or, alternatively, Path 2:

for( const prop of Object.keys(test) ){
    if( prop === 'b' ) return console.log(test[prop])
}  

My question: under the hood, is doing Path 1 essentially the same as Path 2? Does the computer still have to look through every property of test to find b? Or does it somehow "magically" know where exactly to look in the memory when it gets test[b] so it doesn't have to scan through the entire object?

0 Answers
Related