0
0
Supabasecloud~5 mins

Calling external APIs from Edge Functions in Supabase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Calling external APIs from Edge Functions
O(n)
Understanding Time Complexity

When Edge Functions call external APIs, the time taken depends on how many calls are made.

We want to know how the number of API calls grows as input size grows.

Scenario Under Consideration

Analyze the time complexity of this Edge Function calling an external API multiple times.

import { serve } from 'std/server'

serve(async (req) => {
  const data = await req.json()
  const results = []
  for (const item of data.items) {
    const response = await fetch(`https://api.example.com/data/${item.id}`)
    const json = await response.json()
    results.push(json)
  }
  return new Response(JSON.stringify(results))
})

This function loops over input items and calls an external API for each one, collecting results.

Identify Repeating Operations

Look at what repeats as input grows:

  • Primary operation: One external API call per input item.
  • How many times: Exactly once for each item in the input list.
How Execution Grows With Input

Each new item adds one more API call, so the total calls grow directly with input size.

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

Pattern observation: The number of calls grows in a straight line as input grows.

Final Time Complexity

Time Complexity: O(n)

This means the time grows directly in proportion to the number of input items.

Common Mistake

[X] Wrong: "Calling multiple APIs inside a loop is always constant time because calls happen fast."

[OK] Correct: Each API call takes time and adds up, so more calls mean more total time.

Interview Connect

Understanding how external API calls scale helps you design efficient cloud functions and shows you think about real-world delays.

Self-Check

What if we changed the code to call the external API only once with all item IDs at once? How would the time complexity change?