0
0
Firebasecloud~5 mins

Why Firebase exists - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why Firebase exists
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

Each user data save or read is one operation. More users mean more operations.

Input Size (n)Approx. API Calls/Operations
1010 API calls
100100 API calls
10001000 API calls

Pattern observation: The number of operations grows directly with the number of users.

Final Time Complexity

Time Complexity: O(n)

This means if you double the number of users, the work doubles too.

Common Mistake

[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.

Interview Connect

Understanding how Firebase handles many operations helps you design apps that scale well and stay responsive.

Self-Check

"What if we batch multiple user writes into one call? How would the time complexity change?"