HTTP trigger functions in Firebase - Time & Space Complexity
When using HTTP trigger functions in Firebase, it's important to understand how the time to handle requests grows as more data or users interact with the function.
We want to know how the function's execution time changes when it processes different amounts of data or requests.
Analyze the time complexity of the following Firebase HTTP trigger function.
exports.getUsers = functions.https.onRequest(async (req, res) => {
const snapshot = await admin.firestore().collection('users').get();
const users = [];
snapshot.forEach(doc => {
users.push(doc.data());
});
res.send(users);
});
This function fetches all documents from the 'users' collection and sends them back in the response.
Look at what repeats as the input grows.
- Primary operation: Looping through each user document in the collection.
- How many times: Once for every user document stored in the database.
As the number of users increases, the function spends more time reading and processing each user.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 document reads and pushes |
| 100 | 100 document reads and pushes |
| 1000 | 1000 document reads and pushes |
Pattern observation: The work grows directly with the number of users; doubling users doubles the work.
Time Complexity: O(n)
This means the time to complete the function grows in a straight line with the number of user documents.
[X] Wrong: "The function runs in constant time because it just sends a response once."
[OK] Correct: Even though the response is sent once, the function must first read and process each user document, so the time depends on how many users there are.
Understanding how your function's time grows with data size shows you can write efficient backend code that scales well as your app grows.
What if we changed the function to only fetch users with a specific property instead of all users? How would the time complexity change?
Practice
Solution
Step 1: Understand HTTP trigger functions
HTTP trigger functions run when someone visits a specific web address (URL).Step 2: Compare options with this behavior
Only Runs code when a specific web URL is visited describes running code on visiting a URL, matching the definition.Final Answer:
Runs code when a specific web URL is visited -> Option BQuick Check:
HTTP trigger = run code on URL visit [OK]
- Confusing HTTP triggers with database triggers
- Thinking HTTP triggers run automatically without requests
- Assuming HTTP triggers send emails by default
Solution
Step 1: Identify HTTP trigger syntax
HTTP triggers use functions.https.onRequest with (req, res) parameters.Step 2: Match options to this syntax
Only exports.myFunc = functions.https.onRequest((req, res) => { res.send('Hello'); }); uses functions.https.onRequest correctly; others are for database, auth, or storage triggers.Final Answer:
exports.myFunc = functions.https.onRequest((req, res) => { res.send('Hello'); }); -> Option CQuick Check:
HTTP trigger uses functions.https.onRequest [OK]
- Using database or auth triggers instead of HTTP triggers
- Missing (req, res) parameters in function
- Confusing storage triggers with HTTP triggers
exports.greet = functions.https.onRequest((req, res) => { const name = req.query.name || 'Guest'; res.send(`Hello, ${name}!`); });URL visited:
https://us-central1-project.cloudfunctions.net/greet?name=AliceSolution
Step 1: Understand query parameter usage
The function reads the 'name' from the URL query string; if missing, it uses 'Guest'.Step 2: Check the URL query
The URL has '?name=Alice', so name = 'Alice'. The response is 'Hello, Alice!'.Final Answer:
Hello, Alice! -> Option AQuick Check:
Query name=Alice -> Hello, Alice! [OK]
- Ignoring query parameters and using default always
- Assuming undefined if parameter missing
- Expecting error instead of default value
exports.testFunc = functions.https.onRequest((request, response) => { response.send('Test'); response.send('Done'); });Solution
Step 1: Check response.send() usage
response.send() ends the HTTP response. Calling it twice causes an error.Step 2: Analyze the code flow
The function calls response.send('Test') then immediately calls response.send('Done'), which is invalid.Final Answer:
Calling response.send() twice causes an error -> Option DQuick Check:
Only one response.send() allowed per request [OK]
- Calling response.send() multiple times
- Thinking parameter names must be req, res exactly
- Ignoring that response ends after send
age and responds with {"canVote": true} if age is 18 or more, otherwise {"canVote": false}. Which code snippet correctly implements this?Solution
Step 1: Identify correct data source and response method
The age is sent in JSON body, so use req.body.age. To send JSON response, use res.json().Step 2: Check conditions and response format
exports.checkVote = functions.https.onRequest((req, res) => { const age = req.body.age; if(age >= 18) { res.json({canVote: true}); } else { res.json({canVote: false}); } }); correctly checks age >= 18 and sends JSON object with canVote true or false using res.json().Final Answer:
exports.checkVote = functions.https.onRequest((req, res) => { const age = req.body.age; if(age >= 18) { res.json({canVote: true}); } else { res.json({canVote: false}); } }); -> Option AQuick Check:
Use req.body and res.json() for JSON input/output [OK]
- Using req.query instead of req.body for JSON data
- Sending JSON as string instead of object
- Using res.send() with object without res.json()
Ready to prove it?
Pass the test and this pattern moves unknown
