Firebase with Angular - Time & Space Complexity
When using Firebase with Angular, it's important to understand how the number of data requests affects performance.
We want to know how the time to get data grows as we ask for more items.
Analyze the time complexity of this Firebase data fetching in Angular.
// Angular service method to fetch user profiles
getUserProfiles(userIds: string[]) {
return Promise.all(userIds.map(id =>
this.firestore.collection('users').doc(id).get().toPromise()
));
}
This code fetches user profiles one by one from Firebase Firestore using Angular.
Look at what repeats when fetching multiple users.
- Primary operation: One Firestore document read per user ID.
- How many times: Once for each user ID in the list.
Each user ID causes one separate request to Firebase.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 document reads |
| 100 | 100 document reads |
| 1000 | 1000 document reads |
As you ask for more users, the number of requests grows directly with the number of users.
Time Complexity: O(n)
This means the time to fetch data grows in a straight line with how many users you ask for.
[X] Wrong: "Fetching multiple users at once only takes one request."
[OK] Correct: Each user profile is a separate document, so each needs its own request, making total requests grow with user count.
Understanding how data fetching scales helps you design apps that stay fast as they grow, a key skill in cloud development.
"What if we changed to fetching all user profiles with a single query instead of one by one? How would the time complexity change?"