Deleting fields in Firebase - Time & Space 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.
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.
- 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.
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 |
|---|---|
| 10 | 1 update call deleting 10 fields |
| 100 | 1 update call deleting 100 fields |
| 1000 | 1 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.
Time Complexity: O(n)
This means the time to delete fields grows linearly with how many fields you delete in one update.
[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.
Understanding how batch updates scale helps you design efficient data operations and shows you can think about performance beyond just counting API calls.
"What if we delete fields one by one with separate update calls? How would the time complexity change?"