Uploading files from frontend in Supabase - Time & Space Complexity
When uploading files from the frontend using Supabase, it is important to understand how the time taken grows as you upload more files or larger files.
We want to know how the number of upload operations changes as the input size increases.
Analyze the time complexity of the following operation sequence.
const uploadFiles = async (files) => {
for (const file of files) {
const { data, error } = await supabase.storage
.from('uploads')
.upload(file.name, file)
if (error) {
console.error('Upload failed:', error)
}
}
}
This code uploads each file one by one to Supabase storage from the frontend.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The
uploadAPI call to send each file to storage. - How many times: Once for each file in the input list.
As the number of files increases, the number of upload calls increases directly.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 upload calls |
| 100 | 100 upload calls |
| 1000 | 1000 upload calls |
Pattern observation: The number of upload operations grows linearly with the number of files.
Time Complexity: O(n)
This means the time to upload grows directly in proportion to the number of files you upload.
[X] Wrong: "Uploading multiple files at once takes the same time as uploading one file."
[OK] Correct: Each file requires a separate upload call and data transfer, so more files mean more time.
Understanding how upload time grows with file count helps you design better user experiences and backend handling in real projects.
"What if we upload all files in parallel instead of one by one? How would the time complexity change?"
Practice
supabase.storage.from(bucket).upload(path, file) method do in a frontend app?Solution
Step 1: Understand the method purpose
The methodupload(path, file)is used to send a file from the frontend to the backend storage.Step 2: Identify the action on storage
Since it usesupload, it means adding or saving a file, not deleting or downloading.Final Answer:
Uploads a file to the specified storage bucket at the given path -> Option BQuick Check:
Upload method = Upload file [OK]
- Confusing upload with download or delete
- Thinking it lists files instead of uploading
- Ignoring the bucket and path parameters
myFile to the bucket avatars at path user1/profile.png using Supabase storage in frontend JavaScript?Solution
Step 1: Check method chaining and parameters
The correct method chain issupabase.storage.from(bucket).upload(path, file).Step 2: Match parameters order
The first argument touploadis the path string, second is the file object.Final Answer:
supabase.storage.from('avatars').upload('user1/profile.png', myFile) -> Option AQuick Check:
Correct method and parameter order = supabase.storage.from('avatars').upload('user1/profile.png', myFile) [OK]
- Swapping file and path arguments
- Using upload directly on storage without from()
- Incorrect method chaining order
const { data, error } = await supabase.storage.from('docs').upload('file.txt', fileInput.files[0]);
if (error) {
console.log('Upload failed');
} else {
console.log('Upload succeeded:', data);
}Solution
Step 1: Understand the upload response
On success,datacontains info about the uploaded file, including its path.Step 2: Check error handling
Iferroris null, the else block runs and logs success withdata.Final Answer:
Upload succeeded: { path: 'file.txt', ... } -> Option AQuick Check:
Success logs data object = Upload succeeded: { path: 'file.txt', ... } [OK]
- Assuming data is undefined on success
- Confusing error presence with success
- Thinking await causes syntax error outside async
const { data, error } = supabase.storage.from('images').upload('pic.png', file);
if (error) {
console.log('Upload failed');
} else {
console.log('Upload succeeded');
}Solution
Step 1: Check async usage
Theuploadmethod returns a promise and must be awaited to getdataanderror.Step 2: Identify missing await effect
Withoutawait,{ data, error }destructures the unresolved Promise, leaving bothundefined. Thusif (error)is false, incorrectly logging success.Final Answer:
Missing await before supabase.storage.from(...).upload(...) -> Option CQuick Check:
Async calls need await to get results [OK]
- Not awaiting the upload promise
- Assuming upload is synchronous
- Ignoring file variable existence
uploads and keep their original filenames. Which approach correctly uploads all files and handles errors properly?Solution
Step 1: Understand multiple file uploads
Supabase storageuploadhandles one file at a time, so loop over files.Step 2: Handle async and errors properly
Useawaitinside the loop to wait for each upload and check for errors to inform the user.Final Answer:
Use a loop with await inside to upload each file and check errors after each upload -> Option DQuick Check:
Upload files one by one with await and error check [OK]
- Passing array directly to upload method
- Not awaiting uploads causing race conditions
- Overwriting files by using same filename
Ready to prove it?
Pass the test and this pattern moves unknown
