Upload progress monitoring in Firebase - Time & Space Complexity
When uploading files with Firebase, tracking progress helps users see how much is done.
We want to understand how the time to update progress changes as the file size grows.
Analyze the time complexity of the following Firebase upload progress code.
const uploadTask = storageRef.put(file);
uploadTask.on('state_changed', snapshot => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
});
This code listens to upload progress events and calculates the percentage uploaded.
Look for repeated actions during the upload.
- Primary operation: The progress event handler runs repeatedly as data uploads.
- How many times: Number of times depends on how many chunks the file is split into during upload.
As the file size grows, the number of progress updates grows roughly in proportion.
| Input Size (n in MB) | Approx. Progress Events |
|---|---|
| 10 | About 10-20 events |
| 100 | About 100-200 events |
| 1000 | About 1000-2000 events |
Pattern observation: More data means more progress events roughly in direct proportion.
Time Complexity: O(n)
This means the number of progress updates grows linearly with the file size.
[X] Wrong: "Progress updates happen only once or twice regardless of file size."
[OK] Correct: The upload sends data in chunks, so progress updates happen many times, increasing with file size.
Understanding how progress updates scale helps you design smooth user experiences and efficient event handling in real projects.
What if the upload used larger chunk sizes? How would the time complexity of progress updates change?
Practice
state_changed event in Firebase Storage upload tasks help you monitor?Solution
Step 1: Understand the purpose of
Thestate_changedstate_changedevent triggers updates during the upload process.Step 2: Identify what is monitored
This event provides progress updates, such as bytes transferred and total bytes.Final Answer:
The progress of the file upload -> Option DQuick Check:
Upload progress = state_changed event [OK]
- Confusing upload progress with download speed
- Thinking it tracks file size before upload
- Assuming it counts files in storage
Solution
Step 1: Recall Firebase Storage upload syntax
The method to listen for upload events isonwith event namestate_changed.Step 2: Match correct method and event
OnlyuploadTask.on('state_changed', progressCallback)is valid syntax.Final Answer:
uploadTask.on('state_changed', progressCallback) -> Option AQuick Check:
Use .on('state_changed') for progress [OK]
- Using non-existent methods like listen or track
- Wrong event names like 'progress' or 'upload'
- Confusing event listener syntax with DOM events
uploadTask.on('state_changed', (snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log(`Upload is ${progress}% done`);
});Solution
Step 1: Calculate progress percentage
Progress = (500 / 1000) * 100 = 50%Step 2: Understand console output
The console logs: "Upload is 50% done".Final Answer:
Upload is 50% done -> Option BQuick Check:
Progress = bytesTransferred / totalBytes * 100 [OK]
- Confusing bytesTransferred with percentage
- Multiplying by 100 twice
- Using integer division without decimal
uploadTask.on('state_change', (snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log(`Upload is ${progress}% done`);
});
What is the error?Solution
Step 1: Check event name spelling
The correct event name is 'state_changed', not 'state_change'.Step 2: Understand impact of wrong event name
Using wrong event name means callback never triggers, so no logs appear.Final Answer:
The event name should be 'state_changed', not 'state_change' -> Option AQuick Check:
Event name must be exact 'state_changed' [OK]
- Misspelling event names
- Expecting logs without callback firing
- Confusing method names like addListener vs on
state_changed event?Solution
Step 1: Use correct event and callbacks
Thestate_changedevent accepts three callbacks: progress, error, and completion.Step 2: Verify progress calculation and completion message
uploadTask.on('state_changed', snapshot => { const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log(`Upload is ${progress}% done`); }, error => { console.error(error); }, () => { console.log('Upload complete!'); }); calculates progress and logs completion correctly; others miss callbacks or use wrong event.Final Answer:
uploadTask.on('state_changed', snapshot => { const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log(`Upload is ${progress}% done`); }, error => { console.error(error); }, () => { console.log('Upload complete!'); }); -> Option CQuick Check:
Use three callbacks with 'state_changed' event [OK]
- Using wrong event names like 'progress'
- Missing completion callback
- Logging completion inside progress callback
Ready to prove it?
Pass the test and this pattern moves unknown
