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?"