0
0
Firebasecloud~5 mins

Batch writes in Firebase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Batch writes
O(n)
Understanding Time Complexity

When using batch writes in Firebase, it's important to know how the time to complete changes as you add more writes.

We want to understand how the number of write operations affects the total time taken.

Scenario Under Consideration

Analyze the time complexity of the following batch write operation.

const batch = firestore.batch();
const docs = [doc1, doc2, doc3, /* ... */ docN];

for (const doc of docs) {
  batch.set(doc.ref, doc.data());
}

await batch.commit();

This code groups multiple document writes into one batch and sends them together to Firebase.

Identify Repeating Operations

Look at what repeats as the input grows.

  • Primary operation: Adding each write to the batch with batch.set().
  • How many times: Once for each document to write (n times).
  • Dominant operation: The batch.commit() sends all writes at once, but the batch size affects the total work.
How Execution Grows With Input

As you add more documents, the number of write operations grows directly with the number of documents.

Input Size (n)Approx. Api Calls/Operations
1010 writes added, 1 batch commit
100100 writes added, 1 batch commit
10001000 writes added, 1 batch commit

Pattern observation: The number of write additions grows linearly with input size, but the commit is a single call.

Final Time Complexity

Time Complexity: O(n)

This means the total time grows roughly in direct proportion to the number of writes you add to the batch.

Common Mistake

[X] Wrong: "Batch writes always take the same time no matter how many writes are included."

[OK] Correct: While the commit is one call, adding more writes means more work inside the batch, so time grows with the number of writes.

Interview Connect

Understanding how batch writes scale helps you design efficient data updates and shows you can reason about cloud operation costs clearly.

Self-Check

What if we split a large batch into multiple smaller batches? How would that affect the time complexity?