Creating Edge Functions with Deno in Supabase - Performance & Efficiency
When creating Edge Functions with Deno on Supabase, it's important to understand how the time to deploy and execute these functions changes as you add more functions or handle more requests.
We want to know how the number of functions or requests affects the total time spent.
Analyze the time complexity of deploying multiple Edge Functions and handling requests.
// Deploy multiple edge functions
for (let i = 0; i < n; i++) {
await supabase.functions.deploy(`function${i}`, `./functions/function${i}`)
}
// Handle requests to a single function
const response = await supabase.functions.invoke('function0', { body: requestData })
This sequence deploys n Edge Functions one by one, then invokes one function to handle a request.
Look at what repeats as input size grows.
- Primary operation: Deploying each Edge Function via
supabase.functions.deploy. - How many times: Exactly n times, once per function.
- Invocation operation: Calling
supabase.functions.invokeonce, regardless of n.
As you add more functions, deployment time grows because each function is deployed separately.
| Input Size (n) | Approx. Deploy Calls |
|---|---|
| 10 | 10 deploy calls |
| 100 | 100 deploy calls |
| 1000 | 1000 deploy calls |
Deployment time grows directly with the number of functions. Invoking a single function stays the same time regardless of n.
Time Complexity: O(n)
This means the total deployment time grows linearly as you add more Edge Functions.
[X] Wrong: "Deploying multiple functions happens all at once, so time stays the same no matter how many functions I add."
[OK] Correct: Each function deploy is a separate action that takes time, so total time adds up as you deploy more functions.
Understanding how deployment and invocation scale helps you design efficient cloud workflows and shows you can think about real-world costs in cloud projects.
What if you deployed all functions in parallel instead of one by one? How would the time complexity change?
Practice
Solution
Step 1: Understand Edge Functions role
Edge Functions run code near users to reduce delay and improve speed.Step 2: Compare options
Only To run code close to users for faster response times describes running code close to users for faster responses, which matches Edge Functions purpose.Final Answer:
To run code close to users for faster response times -> Option AQuick Check:
Edge Functions = run code near users [OK]
- Confusing Edge Functions with database storage
- Thinking Edge Functions build UI
- Assuming Edge Functions manage schemas
serve method?Solution
Step 1: Recall Deno serve syntax
Deno's serve function takes a handler function returning a Response object.Step 2: Check options
serve(() => new Response('Hello from Edge!')) correctly calls serve with a function returning new Response. Others misuse syntax or order.Final Answer:
serve(() => new Response('Hello from Edge!')) -> Option DQuick Check:
serve(handler returning Response) = correct [OK]
- Assigning serve instead of calling it
- Using Response without new keyword
- Passing string directly instead of Response
import { serve } from "https://deno.land/std/http/server.ts";
serve(() => new Response('Edge Function Active'));Solution
Step 1: Analyze the code
The serve function is imported correctly and called with a handler returning Response with text 'Edge Function Active'.Step 2: Determine output
When a request arrives, the handler returns Response with body 'Edge Function Active', so that text is sent back.Final Answer:
Edge Function Active -> Option AQuick Check:
serve returns Response text = 'Edge Function Active' [OK]
- Assuming serve is undefined without import
- Expecting no response without explicit return
- Confusing Response object with string
import { serve } from "https://deno.land/std/http/server.ts";
serve(() => Response('Hello'));Solution
Step 1: Check Response usage
Response is a class and must be instantiated with 'new'. The code calls Response() without 'new'.Step 2: Verify other parts
serve is imported correctly, arrow function syntax is valid, and Response is allowed in Edge Functions.Final Answer:
Missing 'new' keyword before Response -> Option BQuick Check:
Response needs 'new' keyword [OK]
- Forgetting 'new' before Response
- Assuming serve import is wrong
- Thinking arrow function syntax is incorrect
serve?Solution
Step 1: Understand JSON response requirements
Response body must be a string, so JSON data must be stringified. Headers must specify 'application/json'. Status code 200 is set in options.Step 2: Evaluate options
serve(() => new Response(JSON.stringify({ message: 'Success' }), { headers: { 'Content-Type': 'application/json' }, status: 200 })) correctly stringifies JSON, sets headers and status. serve(() => Response({ message: 'Success' }, { status: 200 })) misses 'new' and stringifies incorrectly. serve(() => new Response({ message: 'Success' }, { headers: { 'Content-Type': 'application/json' } })) passes object directly as body (invalid). serve(() => new Response('Success', { status: 200, headers: { 'Content-Type': 'application/json' } })) sends plain string 'Success' not JSON.Final Answer:
serve(() => new Response(JSON.stringify({ message: 'Success' }), { headers: { 'Content-Type': 'application/json' }, status: 200 })) -> Option CQuick Check:
JSON.stringify + headers + status = correct [OK]
- Passing object directly as body without stringifying
- Omitting 'new' before Response
- Not setting Content-Type header to application/json
Ready to prove it?
Pass the test and this pattern moves unknown
