Resource and request objects in Firebase - Time & Space 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.
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 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.
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 |
|---|---|
| 10 | 10 document fetches |
| 100 | 100 document fetches |
| 1000 | 1000 document fetches |
Pattern observation: The number of API calls grows directly with the number of user IDs.
Time Complexity: O(n)
This means the time to fetch data grows in direct proportion to the number of user IDs.
[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.
Understanding how request counts grow helps you design efficient data fetching in real projects.
"What if we used a batch get request instead of individual getDoc calls? How would the time complexity change?"