User profile data in Firebase - Time & Space Complexity
When working with user profile data in Firebase, it's important to understand how the time to fetch or update profiles changes as the number of users grows.
We want to know how the number of operations changes when we handle more user profiles.
Analyze the time complexity of the following operation sequence.
const db = firebase.firestore();
async function getUserProfiles(userIds) {
const profiles = [];
for (const id of userIds) {
const doc = await db.collection('users').doc(id).get();
profiles.push(doc.data());
}
return profiles;
}
This code fetches user profile data for a list of user IDs one by one from Firestore.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: One Firestore document read per user ID.
- How many times: Once for each user ID in the input list.
Each additional user ID adds one more document read operation.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 document reads |
| 100 | 100 document reads |
| 1000 | 1000 document reads |
Pattern observation: The number of operations grows directly in proportion to the number of user IDs.
Time Complexity: O(n)
This means the time to fetch profiles grows linearly with the number of users requested.
[X] Wrong: "Fetching multiple user profiles is just one operation regardless of how many users."
[OK] Correct: Each user profile requires a separate document read, so the total operations increase with the number of users.
Understanding how data fetching scales helps you design efficient cloud apps and shows you can think about performance as your app grows.
"What if we fetched all user profiles using a single query with a 'where' clause instead of individual document reads? How would the time complexity change?"
Practice
Solution
Step 1: Understand user profile data purpose
User profile data holds personal details like name and email for each user.Step 2: Identify Firebase Firestore role
Firestore stores this data securely and allows easy access and updates.Final Answer:
To save personal information for each user securely -> Option DQuick Check:
User profile data = personal info storage [OK]
- Confusing Firestore with hosting or email services
- Thinking Firestore runs backend code automatically
- Assuming user profiles are for sending emails
Solution
Step 1: Review Firestore document creation syntax
Use collection('name').doc('id').set(data) to create or overwrite a document.Step 2: Match syntax with options
db.collection('users').doc('user123').set({name: 'Alice'}) matches correct syntax: collection, doc, then set with data object.Final Answer:
db.collection('users').doc('user123').set({name: 'Alice'}) -> Option AQuick Check:
Correct Firestore doc creation = db.collection('users').doc('user123').set({name: 'Alice'}) [OK]
- Using add() with document ID specified
- Wrong method names like create() instead of set()
- Incorrect chaining of methods
const docRef = db.collection('users').doc('user999');
docRef.get().then(doc => {
if (doc.exists) {
console.log(doc.data().name);
} else {
console.log('No user found');
}
});Solution
Step 1: Understand doc.exists property
doc.exists is false if document does not exist in Firestore.Step 2: Follow code logic for missing document
If doc.exists is false, code logs 'No user found'.Final Answer:
No user found -> Option AQuick Check:
Missing doc triggers else block = 'No user found' [OK]
- Assuming doc.data() returns null or undefined
- Expecting an error instead of else block output
- Ignoring the else condition
db.collection('users').doc('user123').update({
username: 'Bob'
});Solution
Step 1: Check Firestore update method usage
update() returns a promise and should be handled with await or then.Step 2: Identify missing promise handling
Code does not handle promise, which can cause silent failures.Final Answer:
Missing await or then to handle promise -> Option BQuick Check:
Firestore update returns promise, must handle it [OK]
- Thinking update() cannot change fields
- Assuming field names must be fixed
- Believing document IDs must be numeric
Solution
Step 1: Understand optional fields in Firestore
Firestore allows documents to omit fields that are not provided.Step 2: Choose best practice for optional data
Including the phone number field only when available avoids storing empty or null values and reduces confusion.Final Answer:
Include phone number field only if user provides it -> Option CQuick Check:
Omit optional fields if missing to keep data clean [OK]
- Storing empty strings which can confuse queries
- Using null values that may cause unexpected behavior
- Overcomplicating with separate collections for simple optional data
Ready to prove it?
Pass the test and this pattern moves unknown
