0
0
Rest APIprogramming~5 mins

Composite operations (multi-resource) in Rest API - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Composite operations (multi-resource)
O(n)
Understanding Time Complexity

When a REST API performs multiple resource calls in one operation, the total time depends on all those calls combined.

We want to know how the total work grows as we add more resources to handle.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

async function compositeOperation(resources) {
  const results = [];
  for (const resource of resources) {
    const response = await fetch(`/api/${resource}`);
    const data = await response.json();
    results.push(data);
  }
  return results;
}

This code fetches data from multiple API endpoints one after another and collects the results.

Identify Repeating Operations
  • Primary operation: Fetching each resource from the API.
  • How many times: Once for each resource in the input list.
How Execution Grows With Input

Each additional resource adds one more fetch call, so the total work grows directly with the number of resources.

Input Size (n)Approx. Operations
1010 fetch calls
100100 fetch calls
10001000 fetch calls

Pattern observation: The total work increases in a straight line as more resources are added.

Final Time Complexity

Time Complexity: O(n)

This means the total time grows proportionally with the number of resources we fetch.

Common Mistake

[X] Wrong: "Fetching multiple resources at once is always faster and takes constant time."

[OK] Correct: Each resource still requires a separate fetch call, so total time grows with how many resources there are, even if done sequentially.

Interview Connect

Understanding how multiple API calls add up helps you design efficient systems and explain your reasoning clearly in interviews.

Self-Check

"What if we changed the code to fetch all resources in parallel instead of one by one? How would the time complexity change?"