Resource and request objects in Firebase - Time & Space Complexity
When working with Firebase, we often handle resource and request objects to manage data and actions.
We want to understand how the time to process these objects grows as we handle more requests or resources.
Analyze the time complexity of the following operation sequence.
const db = getFirestore();
async function fetchUserData(userIds) {
const results = [];
for (const id of userIds) {
const docRef = doc(db, 'users', id);
const docSnap = await getDoc(docRef);
results.push(docSnap.data());
}
return results;
}
This code fetches user data for each user ID by creating resource objects and making requests one by one.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Calling
getDoc()to fetch each user document. - How many times: Once for each user ID in the input list.
As the number of user IDs grows, the number of document fetch requests grows at the same rate.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 document fetches |
| 100 | 100 document fetches |
| 1000 | 1000 document fetches |
Pattern observation: The number of API calls grows directly with the number of user IDs.
Time Complexity: O(n)
This means the time to fetch data grows in direct proportion to the number of user IDs.
[X] Wrong: "Fetching multiple documents at once takes the same time as fetching one document."
[OK] Correct: Each document fetch is a separate request, so total time adds up with more documents.
Understanding how request counts grow helps you design efficient data fetching in real projects.
"What if we used a batch get request instead of individual getDoc calls? How would the time complexity change?"
Practice
request object in Firebase functions mainly contain?Solution
Step 1: Understand the role of request objects
The request object holds details about the user's input and request method.Step 2: Differentiate from resource objects
Resource objects describe the trigger data or file, not user input.Final Answer:
Information about what the user sent and how -> Option DQuick Check:
Request object = user input info [OK]
- Confusing resource object with request object
- Thinking request holds trigger file info
- Assuming request contains project settings
Solution
Step 1: Recall request object structure
In Firebase HTTPS functions, user data is insiderequest.body.Step 2: Identify correct property for user data
The user data is accessed asrequest.body.dataif data is nested inside body.Final Answer:
request.body.data -> Option BQuick Check:
Access user data via request.body.data [OK]
- Using request.data.body which is invalid
- Trying to access request.payload which doesn't exist
- Confusing trigger data with request data
exports.myFunction = functions.storage.object().onFinalize((object, context) => {
console.log(object.name);
console.log(context.eventId);
});What does the
object parameter represent?Solution
Step 1: Identify the trigger type
The function triggers on a storage object finalization, meaning a file event.Step 2: Understand the object parameter
Theobjectparameter holds info about the file that triggered the function.Final Answer:
The file that triggered the function -> Option CQuick Check:
Storage trigger object = file info [OK]
- Thinking object is user request data
- Confusing context with resource object
- Assuming object holds config or logs
exports.func = functions.https.onRequest((resource, request) => {
console.log(request.body);
});Solution
Step 1: Check HTTPS function parameters
HTTPS functions receive (request, response) parameters in that order.Step 2: Identify parameter misuse
The code uses (resource, request) which reverses and misnames parameters.Final Answer:
Parameters are reversed; should be (request, response) -> Option AQuick Check:
HTTPS function params = (request, response) [OK]
- Swapping request and response parameters
- Using resource parameter in HTTPS function
- Ignoring parameter order importance
Solution
Step 1: Understand Firestore trigger parameters
Firestore triggers receive achangeobject withbeforeandaftersnapshots.Step 2: Access the new document data
The updated document data is accessed viachange.after.data(), which is the resource object.Final Answer:
resource.after.data() -> Option AQuick Check:
Firestore change after data = resource.after.data() [OK]
- Using request.body which is for HTTP functions
- Using context.params which holds path variables
- Using resource.data() which is old snapshot, not updated
Ready to prove it?
Pass the test and this pattern moves unknown
