Accessing object properties in Javascript - Time & Space Complexity
When we access properties in an object, we want to know how long it takes as the object grows.
We ask: Does looking up a property take more time if the object has many properties?
Analyze the time complexity of the following code snippet.
const person = {
name: 'Alice',
age: 30,
city: 'Wonderland'
};
const city = person.city;
console.log(city);
This code accesses the city property from the person object and prints it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing a single property in an object.
- How many times: Once in this example.
Accessing a property takes about the same time no matter how many properties the object has.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant even if the object grows bigger.
Time Complexity: O(1)
This means accessing a property takes the same amount of time no matter how big the object is.
[X] Wrong: "Accessing a property takes longer if the object has more properties."
[OK] Correct: JavaScript engines use fast methods to find properties, so the time does not grow with object size.
Understanding that property access is fast helps you reason about code performance and write efficient programs.
"What if we accessed a property inside a nested object? How would the time complexity change?"
Practice
age in a JavaScript object person when the property name is known and simple?Solution
Step 1: Understand dot notation usage
Dot notation is used when the property name is a simple, valid identifier known in advance.Step 2: Apply dot notation to access property
Usingperson.ageaccesses theageproperty directly.Final Answer:
person.age -> Option BQuick Check:
Dot notation = person.age [OK]
- Using arrow (->) instead of dot
- Using parentheses like a function call
- Using bracket notation unnecessarily
"first-name" of an object user?Solution
Step 1: Identify property name with special characters
The property name"first-name"contains a hyphen, which is not valid for dot notation.Step 2: Use bracket notation with quotes
Bracket notation with quotes,user['first-name'], correctly accesses such properties.Final Answer:
user['first-name'] -> Option AQuick Check:
Bracket notation for special chars = user['first-name'] [OK]
- Using dot notation with hyphenated names
- Omitting quotes inside brackets
- Using undefined variable inside brackets
const car = { brand: 'Toyota', year: 2020 };
console.log(car['brand']);Solution
Step 1: Understand bracket notation access
The code uses bracket notation with the string 'brand' to access the property value.Step 2: Retrieve the value of 'brand'
The value ofcar['brand']is 'Toyota', so it prints 'Toyota'.Final Answer:
Toyota -> Option DQuick Check:
car['brand'] = 'Toyota' [OK]
- Confusing property name with value
- Expecting property name to print
- Using wrong quotes causing error
const book = { title: '1984', author: 'Orwell' };
console.log(book[title]);Solution
Step 1: Check bracket notation usage
Inside brackets, property names must be strings or variables. Here,titleis treated as a variable, not a string.Step 2: Fix by adding quotes
Adding quotes:book['title']correctly accesses the property.Final Answer:
Missing quotes around 'title' in bracket notation -> Option CQuick Check:
Bracket keys need quotes if not variables [OK]
- Forgetting quotes around string keys
- Assuming dot notation always required
- Confusing variable and string keys
settings = { 'theme-color': 'dark', fontSize: 14 }, which code correctly changes the theme-color to 'light' and increases fontSize by 2?Solution
Step 1: Update 'theme-color' using bracket notation
Since 'theme-color' has a hyphen, use bracket notation:settings['theme-color'] = 'light';Step 2: Increase fontSize using dot notation
fontSize is a simple property, sosettings.fontSize += 2;correctly adds 2.Final Answer:
settings['theme-color'] = 'light'; settings.fontSize += 2; -> Option AQuick Check:
Bracket for special keys + dot for simple keys [OK]
- Using dot notation for hyphenated keys
- Mistyping += as =+
- Mixing up property names and strings
Ready to prove it?
Pass the test and this pattern moves unknown
