Why Firebase exists - Performance Analysis
We want to understand how the work done by Firebase grows as we use it more.
Specifically, how the number of operations changes when we add more data or users.
Analyze the time complexity of saving and reading user data in Firebase.
const db = firebase.firestore();
// Save user data
function saveUserData(userId, data) {
return db.collection('users').doc(userId).set(data);
}
// Read user data
function getUserData(userId) {
return db.collection('users').doc(userId).get();
}
This code saves and reads data for one user in Firebase Firestore.
Look at what happens when we save or read data for many users.
- Primary operation: One API call to save or read a single user document.
- How many times: Once per user data operation.
Each user data save or read is one operation. More users mean more operations.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The number of operations grows directly with the number of users.
Time Complexity: O(n)
This means if you double the number of users, the work doubles too.
[X] Wrong: "Saving or reading many users is just one operation because Firebase is cloud-based."
[OK] Correct: Each user data save or read is a separate API call, so the total work grows with the number of users.
Understanding how Firebase handles many operations helps you design apps that scale well and stay responsive.
"What if we batch multiple user writes into one call? How would the time complexity change?"