Why file storage is needed in Firebase - Performance Analysis
We want to understand how storing files in Firebase grows in cost as we add more files.
How does the number of files affect the work Firebase does?
Analyze the time complexity of uploading multiple files to Firebase Storage.
const uploadFiles = async (files) => {
for (const file of files) {
const storageRef = firebase.storage().ref().child(file.name);
await storageRef.put(file);
}
};
This code uploads each file one by one to Firebase Storage.
Look at what repeats as we add more files.
- Primary operation: Uploading a single file using
storageRef.put(file). - How many times: Once for each file in the list.
Each new file means one more upload operation.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 uploads |
| 100 | 100 uploads |
| 1000 | 1000 uploads |
Pattern observation: The number of uploads grows directly with the number of files.
Time Complexity: O(n)
This means the work grows in a straight line as you add more files.
[X] Wrong: "Uploading many files takes the same time as uploading one file."
[OK] Correct: Each file needs its own upload, so more files mean more work and time.
Understanding how file uploads scale helps you design apps that handle many files smoothly.
"What if we upload files in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of file storage
File storage is designed to save files such as images, videos, and documents, not just text or code.Step 2: Compare options with file storage use
Storing only text data like messages, running code faster on the device, or replacing the app's database do not describe file storage's role correctly. Only saving and managing files like images and documents in the cloud matches the purpose.Final Answer:
To save and manage files like images and documents in the cloud -> Option AQuick Check:
File storage purpose = save files [OK]
- Thinking file storage speeds up app code
- Confusing file storage with database storage
- Believing file storage only holds text data
Solution
Step 1: Understand Firebase Storage path format
Paths use folders first, then file names, e.g., folder/file.ext.Step 2: Check each option's order
Onlyusers/user123/profile.jpgfollows folder (users/user123) then file (profile.jpg). Others mix order incorrectly.Final Answer:
users/user123/profile.jpg-> Option BQuick Check:
Folder before file name = correct path [OK]
- Putting file name before folders
- Mixing folder and file order
- Using invalid path separators
images/cat.png and then download it?Solution
Step 1: Understand upload and download behavior
Uploading saves the file exactly; downloading retrieves the same file without changes.Step 2: Evaluate each option
Only The filecat.pngis saved and can be retrieved exactly as uploaded correctly describes this behavior. Others describe incorrect or impossible outcomes.Final Answer:
The filecat.pngis saved and can be retrieved exactly as uploaded -> Option DQuick Check:
Upload/download = same file saved and retrieved [OK]
- Thinking files change format automatically
- Assuming files delete after upload
- Believing files can't be downloaded
Solution
Step 1: Identify invalid path issues
Double slashes in paths are invalid and cause errors in Firebase Storage.Step 2: Check other options for errors
Uploading to a folder named images, uploading files smaller than 1MB, or using filenames with only letters and numbers are valid and do not cause upload errors.Final Answer:
Using an invalid file path likeimages//cat.pngwith double slashes -> Option CQuick Check:
Invalid path format = upload error [OK]
- Using invalid path characters
- Assuming small files cause errors
- Thinking normal folder names cause errors
Solution
Step 1: Understand Firebase Storage features
Firebase Storage organizes files in folders and uses security rules to control who can access them.Step 2: Evaluate options for app needs
Saving photos in organized folders and controlling access with security rules matches the need to store and share photos securely. Storing only on device without cloud backup, auto-deleting after 24 hours, or converting to text do not meet these requirements.Final Answer:
It saves photos in organized folders and controls access with security rules -> Option AQuick Check:
Organized storage + security rules = secure sharing [OK]
- Thinking files stay only on device
- Believing files auto-delete quickly
- Assuming files convert to text automatically
Ready to prove it?
Pass the test and this pattern moves unknown
