0
0
Firebasecloud~5 mins

Why Firestore performance needs planning in Firebase - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why Firestore performance needs planning
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations
  • Primary operation: Firestore query to get documents matching a condition.
  • How many times: One query per function call, but the query returns multiple documents.
How Execution Grows With Input

As the number of posts by the user grows, the query returns more documents.

Input Size (n)Approx. Api Calls/Operations
101 query returning 10 documents
1001 query returning 100 documents
10001 query returning 1000 documents

Pattern observation: The number of documents returned grows with n, but the number of queries stays the same.

Final Time Complexity

Time Complexity: O(n)

This means the time to get all posts grows linearly with how many posts the user has.

Common Mistake

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

Interview Connect

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.

Self-Check

"What if we added pagination to limit documents per query? How would the time complexity change?"