Why serverless functions extend Firebase - Performance Analysis
We want to see how the work done by serverless functions grows as we handle more events in Firebase.
How does the number of function calls change when more users or data trigger these functions?
Analyze the time complexity of this serverless function triggered by Firebase events.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.processNewUser = functions.auth.user().onCreate((user) => {
// Perform some processing when a new user signs up
return someAsyncTask(user.uid);
});
function someAsyncTask(uid) {
// Simulate async work like writing to Firestore
return admin.firestore().collection('profiles').doc(uid).set({createdAt: Date.now()});
}
This function runs every time a new user signs up, doing some work like saving data.
Look at what repeats as more users sign up.
- Primary operation: The serverless function call triggered by each new user creation event.
- How many times: Once per new user signup, so it grows with the number of users.
Each new user triggers one function call, so the total calls grow directly with user count.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 function calls |
| 100 | 100 function calls |
| 1000 | 1000 function calls |
Pattern observation: The number of function calls grows linearly as more users sign up.
Time Complexity: O(n)
This means the work grows in direct proportion to the number of new users triggering the function.
[X] Wrong: "The serverless function runs only once no matter how many users sign up."
[OK] Correct: Each user signup triggers a separate function call, so the total work increases with more users.
Understanding how serverless functions scale with events helps you design systems that handle growth smoothly and predict costs.
"What if the function was triggered by batch events instead of single user signups? How would the time complexity change?"