Nested objects in Javascript - Time & Space Complexity
When working with nested objects, it is important to understand how the time to access or process data grows as the object gets bigger.
We want to know how the number of steps changes when the nested objects have more keys or levels.
Analyze the time complexity of the following code snippet.
const data = {
user: {
name: 'Alice',
address: { city: 'Wonderland', zip: 12345 }
},
orders: [ { id: 1 }, { id: 2 } ]
};
function printUserCity(obj) {
console.log(obj.user.address.city);
}
printUserCity(data);
This code accesses a nested property inside an object and prints it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing nested keys step-by-step.
- How many times: Each key access happens once in sequence.
Accessing nested keys takes one step per level, so if the nesting grows deeper, the steps grow linearly with depth.
| Input Size (nesting depth) | Approx. Operations |
|---|---|
| 3 | 3 steps (user → address → city) |
| 5 | 5 steps (5 nested keys) |
| 10 | 10 steps (10 nested keys) |
Pattern observation: The number of steps grows directly with how deep the object is nested.
Time Complexity: O(n)
This means the time to access a nested value grows linearly with the depth of the nesting.
[X] Wrong: "Accessing nested objects is always constant time because it's just property access."
[OK] Correct: Each nested key access happens one after another, so deeper nesting means more steps, not always instant.
Understanding how nested object access scales helps you reason about data structures and performance in real projects.
"What if we added a loop to process all keys at each nesting level? How would the time complexity change?"
Practice
Solution
Step 1: Understand object structure
An object can hold many types of values, including other objects.Step 2: Define nested object
A nested object means one object is stored as a value inside another object.Final Answer:
An object inside another object -> Option BQuick Check:
Nested object = object inside object [OK]
- Thinking nested means array inside object
- Confusing functions with objects
- Assuming nested means string inside object
const data = { info: { age: 42 } };Solution
Step 1: Access nested keys with dot notation
Use dot notation to access nested object properties: data.info.age.Step 2: Check syntax correctness
data[info][age] is wrong because keys must be strings or variables; data['info'.'age'] is invalid syntax; data.info['age']() tries to call a value as function.Final Answer:
data.info.age -> Option AQuick Check:
Dot notation for nested keys = data.info.age [OK]
- Using brackets without quotes for keys
- Trying to call a property as a function
- Incorrect string concatenation in keys
const user = { name: 'Alice', contact: { email: 'alice@example.com', phone: '1234' } };
console.log(user.contact.email);Solution
Step 1: Identify nested property access
user.contact.email accesses the 'email' property inside the 'contact' object.Step 2: Check the value of user.contact.email
The value is the string 'alice@example.com'.Final Answer:
'alice@example.com' -> Option AQuick Check:
Access nested email = 'alice@example.com' [OK]
- Accessing wrong property like user.email
- Using wrong quotes causing syntax error
- Expecting phone instead of email
const book = { title: 'JS Guide', author: { firstName: 'John', lastName: 'Doe' } };
console.log(book.author.firstName());Solution
Step 1: Check property type
book.author.firstName is a string, not a function.Step 2: Identify the error in calling it
Using parentheses () tries to call it as a function, causing a TypeError.Final Answer:
firstName is not a function -> Option CQuick Check:
Calling string as function causes error [OK]
- Calling string property as function
- Ignoring error message
- Assuming missing semicolon causes error
const company = {
name: 'TechCorp',
departments: {
sales: { employees: 10 },
development: { employees: 25 },
hr: { employees: 5 }
}
};
// How to get total employees in all departments?Which code correctly calculates total employees?
Solution
Step 1: Understand object structure
Each department is an object inside company.departments, with an employees property holding a number.Step 2: Access employees count correctly
Access employees by chaining: company.departments.sales.employees, etc., then add them.Step 3: Check incorrect options
Adding the department objects directly (like company.departments.sales) leads to summing objects instead of numbers; accessing employees as a property of departments fails because it doesn't exist on departments--each department has its own.Final Answer:
company.departments.sales.employees + company.departments.development.employees + company.departments.hr.employees -> Option DQuick Check:
Sum employees by chaining keys correctly [OK]
- Adding objects instead of numbers
- Wrong property order
- Using wrong key names
Ready to prove it?
Pass the test and this pattern moves unknown
