0
0
Firebasecloud~5 mins

Uploading files in Firebase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Uploading files
O(n)
Understanding Time Complexity

When uploading files to Firebase Storage, it is important to understand how the time taken grows as the number or size of files increases.

We want to know how the number of upload operations changes when we add more files.

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.


const storageRef = firebase.storage().ref();
const files = [file1, file2, file3];

files.forEach(file => {
  const fileRef = storageRef.child(file.name);
  fileRef.put(file).then(() => {
    console.log(file.name + ' uploaded');
  });
});
    

This code uploads each file in the list to Firebase Storage one by one.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Uploading each file using fileRef.put(file).
  • How many times: Once for each file in the list.
How Execution Grows With Input

Each additional file adds one more upload operation, so the total work grows directly with the number of files.

Input Size (n)Approx. API Calls/Operations
1010 uploads
100100 uploads
10001000 uploads

Pattern observation: The number of upload operations increases linearly as the number of files increases.

Final Time Complexity

Time Complexity: O(n)

This means the time to upload grows in direct proportion to the number of files.

Common Mistake

[X] Wrong: "Uploading multiple files happens all at once, so time stays the same no matter how many files."

[OK] Correct: Each file upload is a separate operation that takes time, so more files mean more total upload time.

Interview Connect

Understanding how upload operations scale helps you design efficient file handling in cloud apps and shows you can think about performance clearly.

Self-Check

"What if we upload files in parallel instead of one after another? How would the time complexity change?"