0
0
Firebasecloud~5 mins

Why Firestore is Firebase's primary database - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why Firestore is Firebase's primary database
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations

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

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
1010 document fetches
100100 document fetches
10001000 document fetches

Pattern observation: The number of fetches grows linearly as the input size grows.

Final Time Complexity

Time Complexity: O(n)

This means the time to fetch all documents grows directly in proportion to how many documents you want.

Common Mistake

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

Interview Connect

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.

Self-Check

"What if we changed the code to fetch all documents in a single batch call? How would the time complexity change?"