0
0
Firebasecloud~5 mins

Resource and request objects in Firebase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Resource and request objects
O(n)
Understanding Time Complexity

When working with Firebase, we often handle resource and request objects to manage data and actions.

We want to understand how the time to process these objects grows as we handle more requests or resources.

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.


const db = getFirestore();

async function fetchUserData(userIds) {
  const results = [];
  for (const id of userIds) {
    const docRef = doc(db, 'users', id);
    const docSnap = await getDoc(docRef);
    results.push(docSnap.data());
  }
  return results;
}
    

This code fetches user data for each user ID by creating resource objects and making requests one by one.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Calling getDoc() to fetch each user document.
  • How many times: Once for each user ID in the input list.
How Execution Grows With Input

As the number of user IDs grows, the number of document fetch requests grows at the same rate.

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

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

Final Time Complexity

Time Complexity: O(n)

This means the time to fetch data grows in direct proportion to the number of user IDs.

Common Mistake

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

[OK] Correct: Each document fetch is a separate request, so total time adds up with more documents.

Interview Connect

Understanding how request counts grow helps you design efficient data fetching in real projects.

Self-Check

"What if we used a batch get request instead of individual getDoc calls? How would the time complexity change?"