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?"
Practice
Solution
Step 1: Understand Firestore's real-time update feature
Firestore automatically updates data in real time, so apps stay current without manual refresh.Step 2: Recognize offline support and automatic scaling
Firestore works offline and scales automatically, making it easy to use and reliable.Final Answer:
Because it updates data in real time and works offline -> Option CQuick Check:
Real-time + offline = Firestore primary [OK]
- Thinking Firestore needs manual scaling
- Believing Firestore lacks security rules
- Confusing Firestore with temporary storage
Solution
Step 1: Recall Firestore collection syntax
The correct method to get a collection reference isfirestore.collection('collectionName').Step 2: Check each option's syntax
Only const ref = firestore.collection('users'); uses the correct method name and syntax.Final Answer:
const ref = firestore.collection('users'); -> Option AQuick Check:
Use .collection('name') to get collection [OK]
- Using getCollection instead of collection
- Accessing collection as a property
- Calling collection as a method on collection
const snapshot = await firestore.collection('users').where('age', '>', 26).get();
const names = snapshot.docs.map(doc => doc.data().name);
console.log(names);Solution
Step 1: Analyze the query condition
The query filters users where age is greater than 26.Step 2: Check which users match the condition
Alice is 30 (matches), Bob is 25 (does not match).Final Answer:
['Alice'] -> Option AQuick Check:
Age > 26 returns Alice only [OK]
- Including Bob who is 25
- Returning empty array by mistake
- Returning both users ignoring filter
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
}
}
}Solution
Step 1: Understand the rule condition
The rule allows read and write only if the authenticated user's ID matches the document's userId.Step 2: Check for errors or incorrect logic
The syntax is correct and the logic restricts access properly.Final Answer:
It allows read and write only if user is authenticated and matches userId -> Option BQuick Check:
Auth check with userId match = correct rule [OK]
- Thinking it allows access without auth
- Assuming syntax errors exist
- Believing it denies all access
Solution
Step 1: Identify offline support in Firestore
Firestore caches data locally, so apps work offline without errors.Step 2: Recognize real-time syncing feature
When online, Firestore syncs changes automatically to keep data updated.Step 3: Compare with other options
Options A, B, and D contradict Firestore's key features.Final Answer:
Because Firestore supports offline data persistence and real-time syncing -> Option DQuick Check:
Offline + real-time sync = Firestore best for chat [OK]
- Thinking Firestore needs manual sync
- Believing Firestore lacks offline support
- Confusing Firestore with temporary storage
Ready to prove it?
Pass the test and this pattern moves unknown
