Event triggered functions in GCP - Time & Space Complexity
When using event triggered functions, it is important to understand how the number of events affects the work done by the system.
We want to know how the system's work grows as more events happen.
Analyze the time complexity of the following operation sequence.
// Cloud Function triggered by new file upload to Cloud Storage
exports.processFile = (event, context) => {
const fileName = event.name;
console.log(`Processing file: ${fileName}`);
// Simulate processing
// ...
};
This function runs once for each new file uploaded to a storage bucket, processing that file.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The function execution triggered by each file upload event.
- How many times: Once per file uploaded to the bucket.
Each new file upload causes one function execution, so the total work grows directly with the number of files.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 function executions |
| 100 | 100 function executions |
| 1000 | 1000 function executions |
Pattern observation: The work grows in a straight line as more files are uploaded.
Time Complexity: O(n)
This means the total work increases directly with the number of events triggering the function.
[X] Wrong: "The function runs once no matter how many files are uploaded."
[OK] Correct: Each file upload triggers a separate function execution, so the work grows with the number of files.
Understanding how event-driven functions scale with input helps you design systems that handle growing workloads smoothly.
"What if the function batches multiple file events together before processing? How would the time complexity change?"