0
0
Firebasecloud~5 mins

Deleting documents in Firebase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Deleting documents
O(n)
Understanding Time Complexity

When deleting documents in Firebase, it's important to know how the time it takes grows as you delete more items.

We want to understand how the number of delete operations changes when the number of documents increases.

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.


const deleteDocuments = async (docs) => {
  for (const doc of docs) {
    await firestore.collection('items').doc(doc.id).delete();
  }
};
    

This code deletes each document one by one from the 'items' collection in Firebase.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Calling the delete() method on each document reference.
  • How many times: Once for every document in the input list.
How Execution Grows With Input

Each document requires one delete call, so as the number of documents grows, the number of delete calls grows the same way.

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

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

Final Time Complexity

Time Complexity: O(n)

This means the time to delete documents grows linearly with how many documents you want to delete.

Common Mistake

[X] Wrong: "Deleting multiple documents at once takes the same time as deleting one document."

[OK] Correct: Each document requires its own delete call, so more documents mean more calls and more time.

Interview Connect

Understanding how delete operations scale helps you design efficient data cleanup in real projects and shows you can think about resource use clearly.

Self-Check

"What if we used a batch delete operation instead of deleting documents one by one? How would the time complexity change?"