Why Firestore performance needs planning in Firebase - Performance Analysis
When using Firestore, how fast your app works depends on how you organize data and requests.
We want to know how the number of reads or writes grows as your data or users grow.
Analyze the time complexity of reading multiple documents from Firestore.
const db = firebase.firestore();
async function getUserPosts(userId) {
const postsSnapshot = await db.collection('posts')
.where('authorId', '==', userId)
.get();
return postsSnapshot.docs.map(doc => doc.data());
}
This code fetches all posts written by one user from the Firestore database.
- Primary operation: Firestore query to get documents matching a condition.
- How many times: One query per function call, but the query returns multiple documents.
As the number of posts by the user grows, the query returns more documents.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 1 query returning 10 documents |
| 100 | 1 query returning 100 documents |
| 1000 | 1 query returning 1000 documents |
Pattern observation: The number of documents returned grows with n, but the number of queries stays the same.
Time Complexity: O(n)
This means the time to get all posts grows linearly with how many posts the user has.
[X] Wrong: "Fetching many documents is always fast because it's just one query."
[OK] Correct: Even one query can take longer if it returns many documents, because each document adds to the work.
Understanding how Firestore queries scale helps you design apps that stay fast as they grow. This skill shows you think about real user experience and system limits.
"What if we added pagination to limit documents per query? How would the time complexity change?"