0
0
Firebasecloud~5 mins

Data aggregation patterns in Firebase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Data aggregation patterns
O(n)
Understanding Time Complexity

When collecting data from many places in Firebase, it's important to know how the time to get all data grows as you add more items.

We want to understand how the number of data requests changes when we gather more data.

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.


const db = firebase.firestore();
const userIds = ["user1", "user2", "user3", /* ... */];

async function aggregateUserData() {
  const results = [];
  for (const id of userIds) {
    const doc = await db.collection('users').doc(id).get();
    results.push(doc.data());
  }
  return results;
}

This code fetches data for each user ID one by one from the Firestore database.

Identify Repeating Operations

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

Each additional user ID adds one more document read request.

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

Pattern observation: The number of reads grows directly with the number of user IDs.

Final Time Complexity

Time Complexity: O(n)

This means the time to get all data grows in a straight line as you add more users.

Common Mistake

[X] Wrong: "Fetching multiple documents at once always takes the same time as fetching one."

[OK] Correct: Each document read is a separate request, so more documents mean more time.

Interview Connect

Understanding how data requests grow helps you design better apps and explain your choices clearly in conversations.

Self-Check

"What if we used a single query to fetch all user documents at once? How would the time complexity change?"