Writing data (set, update, push) in Firebase - Time & Space Complexity
When we write data to Firebase, itβs important to know how the time it takes grows as we add more data.
We want to understand how many operations happen when using set, update, or push.
Analyze the time complexity of the following operation sequence.
const dbRef = firebase.database().ref('users');
// Writing a new user with push
const newUserRef = dbRef.push();
newUserRef.set({ name: 'Alice', age: 30 });
// Updating user data
newUserRef.update({ age: 31 });
// Setting data directly
dbRef.child('user123').set({ name: 'Bob', age: 25 });
This code writes new user data, updates existing data, and sets data directly at a path.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Writing data to the database using set, update, or push.
- How many times: Each write call happens once per data item written.
Each write operation sends data once to the database, so the number of operations grows directly with how many writes you do.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 write calls |
| 100 | 100 write calls |
| 1000 | 1000 write calls |
Pattern observation: The number of write operations grows linearly as you add more data.
Time Complexity: O(n)
This means the time to write data grows directly in proportion to how many write operations you perform.
[X] Wrong: "Writing one big set call with all data is always faster than many small writes."
[OK] Correct: Sometimes one big write can be slower or cause more data transfer, and Firebase handles many small writes efficiently. The time depends on data size and network, not just number of calls.
Understanding how write operations scale helps you design apps that handle data smoothly and predict how your app behaves as it grows.
What if we batch multiple updates into one update call? How would the time complexity change?