Nested objects and arrays in Firebase - Time & Space Complexity
When working with nested objects and arrays in Firebase, it's important to understand how the time to read or write data grows as the data size increases.
We want to know how the number of operations changes when the nested data gets bigger.
Analyze the time complexity of the following operation sequence.
const docRef = firestore.collection('users').doc('user123');
const snapshot = await docRef.get();
const nestedData = snapshot.data().orders;
nestedData.forEach(order => {
console.log(order.items.length);
});
This code reads a user's document, accesses a nested array of orders, and then loops through each order to check the number of items inside.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Reading the document once from Firestore.
- How many times: The document read happens once, but iterating over the nested array happens once per order.
As the number of orders grows, the loop runs more times, increasing the operations linearly.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 orders | 1 document read + 10 loop operations |
| 100 orders | 1 document read + 100 loop operations |
| 1000 orders | 1 document read + 1000 loop operations |
Pattern observation: The document read stays the same, but the loop operations grow directly with the number of orders.
Time Complexity: O(n)
This means the time to process grows directly in proportion to the number of nested orders.
[X] Wrong: "Reading the document once means the time is always constant no matter how big the nested data is."
[OK] Correct: Even though the document read is one operation, processing nested arrays inside requires looping through each element, which takes more time as the array grows.
Understanding how nested data affects operation counts helps you explain performance clearly and shows you can reason about real-world data structures.
"What if we changed the nested array to a nested object with keys instead of an array? How would the time complexity change when accessing all items?"
Practice
Solution
Step 1: Understand nested objects concept
Nested objects allow grouping related data inside a single document, making it easier to manage.Step 2: Compare with other options
Options A, B, and D describe different Firebase features unrelated to nested objects.Final Answer:
To group related data inside one document for better organization -> Option BQuick Check:
Nested objects = group related data [OK]
- Confusing nested objects with collections
- Thinking nested objects create separate databases
- Assuming nested objects encrypt data
Solution
Step 1: Identify correct array syntax in Firebase
Arrays in Firebase are defined using square brackets with comma-separated values.Step 2: Evaluate each option
"tags": ["firebase", "cloud"] uses square brackets correctly. Options A and D use curly braces which define objects, not arrays. "tags": "firebase, cloud" is a string, not an array.Final Answer:
"tags": ["firebase", "cloud"] -> Option AQuick Check:
Arrays use square brackets [] [OK]
- Using curly braces {} instead of square brackets []
- Writing arrays as comma-separated strings
- Confusing objects with arrays
{"user": {"name": "Anna", "roles": ["admin", "editor"]}}What will be the value of
user.roles[1]?Solution
Step 1: Understand array indexing in Firebase data
Arrays start at index 0, so roles[0] is "admin" and roles[1] is "editor".Step 2: Identify the value at index 1
The second element (index 1) in the roles array is "editor".Final Answer:
"editor" -> Option CQuick Check:
Index 1 in roles array = "editor" [OK]
- Assuming array indexes start at 1
- Confusing object keys with array indexes
- Thinking roles[1] is undefined
{"profile": {"contacts": {"email": "user@example.com", "phones": ["123-456", "789-012"]}}}Which code snippet correctly accesses the second phone number?
Solution
Step 1: Understand correct dot and bracket notation
Access nested objects with dots and arrays with brackets. Index 1 is second item.Step 2: Evaluate each option
profile.contacts.phones[1] uses correct dot notation and index 1. profile.contacts.phones[2] uses index 2 which is out of range. Options C and D misuse bracket notation for keys.Final Answer:
profile.contacts.phones[1] -> Option DQuick Check:
Use dot for keys, brackets for array index [OK]
- Using wrong array index (2 instead of 1)
- Using brackets incorrectly for object keys
- Mixing quotes and brackets wrongly
Solution
Step 1: Identify correct nested structure for users and their favorite colors
Each user should be an object with a name and an array of favorite colors.Step 2: Evaluate each option
{"users": [{"name": "Tom", "favorites": ["red", "blue"]}, {"name": "Sue", "favorites": ["green"]}]} correctly nests user objects with arrays for favorites. {"users": {"Tom": "red, blue", "Sue": "green"}} uses a string instead of array. {"users": [{"name": "Tom", "favorites": "red, blue"}, {"name": "Sue", "favorites": "green"}]} uses strings for favorites, not arrays. {"users": ["Tom", "Sue"], "favorites": ["red", "blue", "green"]} separates users and favorites incorrectly.Final Answer:
{"users": [{"name": "Tom", "favorites": ["red", "blue"]}, {"name": "Sue", "favorites": ["green"]}]} -> Option AQuick Check:
Nested objects with arrays for lists = {"users": [{"name": "Tom", "favorites": ["red", "blue"]}, {"name": "Sue", "favorites": ["green"]}]} [OK]
- Using strings instead of arrays for lists
- Separating users and favorites incorrectly
- Not nesting favorites inside each user object
Ready to prove it?
Pass the test and this pattern moves unknown
