0
0
Firebasecloud~5 mins

Firebase with Angular - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Firebase with Angular
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

Each user ID causes one separate request to Firebase.

Input Size (n)Approx. Api Calls/Operations
1010 document reads
100100 document reads
10001000 document reads

As you ask for more users, the number of requests grows directly with the number of users.

Final Time Complexity

Time Complexity: O(n)

This means the time to fetch data grows in a straight line with how many users you ask for.

Common Mistake

[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.

Interview Connect

Understanding how data fetching scales helps you design apps that stay fast as they grow, a key skill in cloud development.

Self-Check

"What if we changed to fetching all user profiles with a single query instead of one by one? How would the time complexity change?"