0
0
Firebasecloud~5 mins

Deleting fields in Firebase - Time & Space Complexity

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

When deleting fields in Firebase documents, it's important to know how the time to complete the operation changes as you delete more fields.

We want to understand how the number of fields affects the work Firebase does behind the scenes.

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.


const docRef = firestore.collection('users').doc('user123');

await docRef.update({
  field1: firebase.firestore.FieldValue.delete(),
  field2: firebase.firestore.FieldValue.delete(),
  field3: firebase.firestore.FieldValue.delete(),
});
    

This code deletes three fields from a single document in Firestore.

Identify Repeating Operations
  • Primary operation: One update API call to Firestore that deletes multiple fields.
  • How many times: The update call happens once regardless of number of fields, but each field deletion is included in the update.
How Execution Grows With Input

As you delete more fields in a single update, the work grows roughly in direct proportion to the number of fields.

Input Size (n)Approx. Api Calls/Operations
101 update call deleting 10 fields
1001 update call deleting 100 fields
10001 update call deleting 1000 fields

Pattern observation: The number of API calls stays the same, but the amount of data processed grows linearly with the number of fields deleted.

Final Time Complexity

Time Complexity: O(n)

This means the time to delete fields grows linearly with how many fields you delete in one update.

Common Mistake

[X] Wrong: "Deleting multiple fields in one update is just as fast as deleting one field because it's a single API call."

[OK] Correct: Even though there is one API call, the server processes each field deletion, so more fields mean more work and longer time.

Interview Connect

Understanding how batch updates scale helps you design efficient data operations and shows you can think about performance beyond just counting API calls.

Self-Check

"What if we delete fields one by one with separate update calls? How would the time complexity change?"