Why Firestore is Firebase's primary database - Performance Analysis
We want to understand how Firestore handles data operations as the amount of data grows.
Specifically, how the time to read or write data changes when using Firestore.
Analyze the time complexity of reading multiple documents from Firestore.
const db = firebase.firestore();
const collectionRef = db.collection('users');
async function getUsers(userIds) {
const users = [];
for (const id of userIds) {
const doc = await collectionRef.doc(id).get();
users.push(doc.data());
}
return users;
}
This code fetches user documents one by one from Firestore using their IDs.
Look at what repeats as input grows.
- Primary operation: Fetching a single document with
doc(id).get(). - How many times: Once for each user ID in the input list.
Each user ID causes one document fetch, so the total fetches grow directly with the number of IDs.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 document fetches |
| 100 | 100 document fetches |
| 1000 | 1000 document fetches |
Pattern observation: The number of fetches grows linearly as the input size grows.
Time Complexity: O(n)
This means the time to fetch all documents grows directly in proportion to how many documents you want.
[X] Wrong: "Fetching multiple documents at once is always faster than fetching them one by one."
[OK] Correct: While batch fetching can reduce network overhead, if you fetch documents one by one as in this code, the time grows linearly because each fetch is separate.
Understanding how Firestore scales with data size helps you design apps that stay fast as they grow.
This skill shows you can think about real-world app performance, a key part of cloud development.
"What if we changed the code to fetch all documents in a single batch call? How would the time complexity change?"