0
0
GCPcloud~5 mins

HTTP triggered functions in GCP - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: HTTP triggered functions
O(n)
Understanding Time Complexity

When using HTTP triggered functions, it's important to understand how the number of requests affects the work done.

We want to know how the function's work grows as more HTTP requests come in.

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.

exports.helloHttp = (req, res) => {
  const name = req.query.name || 'World';
  res.status(200).send(`Hello, ${name}!`);
};

This function responds to each HTTP request by sending a greeting message.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Handling each HTTP request and sending a response.
  • How many times: Once per incoming HTTP request.
How Execution Grows With Input

Each new HTTP request causes the function to run once, doing a small fixed amount of work.

Input Size (n)Approx. API Calls/Operations
1010 function executions
100100 function executions
10001000 function executions

Pattern observation: The work grows directly with the number of requests.

Final Time Complexity

Time Complexity: O(n)

This means the total work grows linearly with the number of HTTP requests received.

Common Mistake

[X] Wrong: "The function runs once and handles all requests at the same time."

[OK] Correct: Each HTTP request triggers a separate function execution, so work grows with requests.

Interview Connect

Understanding how serverless functions scale with requests helps you design efficient cloud services and answer real-world questions confidently.

Self-Check

"What if the function made an external API call for each request? How would the time complexity change?"