Calling external APIs from Edge Functions in Supabase - Time & Space 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.
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.
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.
Each new item adds one more API call, so the total calls grow directly with input size.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 calls |
| 100 | 100 calls |
| 1000 | 1000 calls |
Pattern observation: The number of calls grows in a straight line as input grows.
Time Complexity: O(n)
This means the time grows directly in proportion to the number of input items.
[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.
Understanding how external API calls scale helps you design efficient cloud functions and shows you think about real-world delays.
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?