0
0
Firebasecloud~5 mins

Writing data (set, update, push) in Firebase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Writing data (set, update, push)
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
1010 write calls
100100 write calls
10001000 write calls

Pattern observation: The number of write operations grows linearly as you add more data.

Final Time Complexity

Time Complexity: O(n)

This means the time to write data grows directly in proportion to how many write operations you perform.

Common Mistake

[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.

Interview Connect

Understanding how write operations scale helps you design apps that handle data smoothly and predict how your app behaves as it grows.

Self-Check

What if we batch multiple updates into one update call? How would the time complexity change?