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?
Practice
Solution
Step 1: Understand
Theset()behaviorset()method writes data to a specified path and replaces any existing data there.Step 2: Compare with other methods
update()modifies specific fields without replacing all data,push()adds new child nodes, andget()reads data.Final Answer:
set() -> Option CQuick Check:
Replace all data = set() [OK]
- Confusing update() with set() and expecting full replacement
- Using push() when needing to replace data
- Thinking get() writes data
Solution
Step 1: Identify method for unique child addition
Thepush()method adds a new child with a unique key under the specified path.Step 2: Check syntax correctness
Usingpush({ text: 'Hello' })under 'messages' correctly adds a new message with unique key.Final Answer:
firebase.database().ref('messages').push({ text: 'Hello' }) -> Option BQuick Check:
Unique child addition = push() [OK]
- Using set() which overwrites all children
- Using update() which only modifies existing fields
- Using get() which only reads data
firebase.database().ref('users/user1').update({ age: 30 });What will happen to the existing data at 'users/user1' if it currently contains { name: 'Alice', age: 25 }?
Solution
Step 1: Understand update() behavior
Theupdate()method modifies only the specified fields without removing other existing data.Step 2: Apply to given data
Since 'age' is updated to 30, 'name' remains unchanged as 'Alice'.Final Answer:
Only the 'age' field is updated to 30; 'name' remains 'Alice' -> Option AQuick Check:
update() changes fields, keeps others [OK]
- Thinking update() replaces all data like set()
- Confusing push() with update()
- Expecting an error when update() is valid
Solution
Step 1: Understand push() usage
push()adds a new child with a unique key; it does not update existing fields.Step 2: Analyze the goal
To update email without removing other data,update()is correct;push()wrongly adds a new child instead.Final Answer:
Using push() to update email is incorrect -> Option DQuick Check:
push() adds new child, not update fields [OK]
- Using push() to update existing fields
- Using set() which overwrites all data
- Confusing update() and push() roles
Solution
Step 1: Add multiple messages with unique keys
Usepush()under 'chats' to add each message with a unique key without overwriting others.Step 2: Update last active timestamp without overwriting
Useupdate()on the user's data to change the timestamp without removing other fields.Final Answer:
Use push() for messages and update() for last active timestamp -> Option AQuick Check:
push() adds unique children, update() modifies fields [OK]
- Using set() for messages which overwrites all chats
- Using set() for timestamp which removes other user data
- Using update() to add unique children
Ready to prove it?
Pass the test and this pattern moves unknown
