Uploading files in Firebase - Time & Space 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.
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 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.
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 |
|---|---|
| 10 | 10 uploads |
| 100 | 100 uploads |
| 1000 | 1000 uploads |
Pattern observation: The number of upload operations increases linearly as the number of files increases.
Time Complexity: O(n)
This means the time to upload grows in direct proportion to the number of files.
[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.
Understanding how upload operations scale helps you design efficient file handling in cloud apps and shows you can think about performance clearly.
"What if we upload files in parallel instead of one after another? How would the time complexity change?"
Practice
Solution
Step 1: Understand file upload purpose
Uploading files to Firebase Storage means saving them remotely in the cloud.Step 2: Identify the benefit
Files saved in the cloud can be accessed anytime and are backed up safely.Final Answer:
To save files in the cloud for easy access and backup -> Option AQuick Check:
Uploading files = cloud storage [OK]
- Thinking upload deletes local files
- Confusing upload with file conversion
- Believing upload runs files on server
Solution
Step 1: Identify method to create file location
firebase.storage().ref() creates a reference to a storage path.Step 2: Confirm usage before upload
You must create a reference before uploading a file to specify where it goes.Final Answer:
firebase.storage().ref() -> Option AQuick Check:
Reference method = ref() [OK]
- Using upload() directly without ref()
- Confusing download() with upload process
- Trying to delete before uploading
const storageRef = firebase.storage().ref('images/photo.jpg');
storageRef.put(file).then(() => console.log('Upload complete')).catch(() => console.log('Upload failed'));Solution
Step 1: Analyze the upload promise
storageRef.put(file) returns a promise that resolves on success.Step 2: Check the then() handler
On success, it logs 'Upload complete' to the console.Final Answer:
Upload complete -> Option CQuick Check:
Successful upload logs 'Upload complete' [OK]
- Assuming catch() runs on success
- Confusing promise syntax
- Expecting no output on success
const ref = firebase.storage().ref('docs/report.pdf');
ref.put(file).then(() => console.log('Done'));
console.log('Upload started');Solution
Step 1: Check upload method correctness
ref.put(file) is correct to upload files in Firebase Storage.Step 2: Look for error handling
There is no catch() to handle possible upload failures, which is a best practice.Final Answer:
Missing catch() to handle upload errors -> Option DQuick Check:
Always add catch() for upload errors [OK]
- Using ref.upload() instead of ref.put()
- Placing console.log before upload completion
- Thinking ref() is invalid method
Solution
Step 1: Understand the required path format
The file must be stored under 'users/{userId}/profile.jpg', so 'users/abc123/profile.jpg' is correct.Step 2: Verify the code snippet
const ref = firebase.storage().ref('users/abc123/profile.jpg'); ref.put(file); uses the correct path and calls ref.put(file) to upload.Final Answer:
const ref = firebase.storage().ref('users/abc123/profile.jpg'); ref.put(file); -> Option BQuick Check:
Correct path format = users/abc123/profile.jpg [OK]
- Mixing folder order in path
- Placing userId after filename
- Using incorrect slashes or order
Ready to prove it?
Pass the test and this pattern moves unknown
