Next.js Discord

Discord Forum

Cold starts on Edge?

Answered
Vespid wasp posted this in #help-forum
Open in Discord
Vespid waspOP
Hi, I have an API route that I made using Planetscale's database-js library. Here's the code:
import { Client } from "@planetscale/database";

export const runtime = 'edge' // 'nodejs' (default) | 'edge'

const db = new Client({
  url: process.env["DATABASE_URL"],
});

export async function GET() {
  const startTime = new Date().getTime(); // Start timestamp

  const conn = db.connection();
  const subjectList = await conn.execute("SELECT name from subjects"); // Specify the type as Subject[]

  const rows = subjectList.rows; // Get the "rows" property

  const json = JSON.stringify(rows);

  const endTime = new Date().getTime(); // End timestamp
  const durationInMs = endTime - startTime; // Calculate the duration in milliseconds

  console.log(`Execution time with edge: ${durationInMs}ms`); // Print the duration to the console

  return new Response(json, {
    headers: {
      "content-type": "application/json;charset=UTF-8",
      "access-control-allow-origin": "*",
    },
  });
}

I've timed this API call to try and see if this is any faster than using Prisma, however I noticed that I've been getting spikes in the response times and I'm assuming they're cold starts. (I've attached a screenshot).

I'm pretty sure I'm doing something wrong here or misunderstood because from what I know edge functions are suppose to get rid of cold starts. If anyone can help me figure out where I went wrong or if I'm completely misunderstanding edge functions I'd appreciate it.
Answered by Giant panda
That's the local dev server which behaves entirely different that the production workers Vercel uses.
View full answer

9 Replies

Giant panda
That's the local dev server which behaves entirely different that the production workers Vercel uses.
Answer
Vespid waspOP
I thought it could be that, so if I deploy it won't have any cold starts?
Giant panda
Correct.
Vespid waspOP
Ahhh, okay. Thank you so much 🙏
Giant panda
Locally you have a debug build, a dev server that often recompiles on changes and runs everything under Node.
Vespid waspOP
That makes more sense now, would there be any way to benchmark speed differences locally though? As I'm trying to compare Prisma with Database-JS on the edge. Or should I just deploy 2 differnet sites using each and compare it that way?
Giant panda
There was a page made by Lee IIRC, I can't find it atm
But deploying yourself is the easiest, since I can't find the benchmark anymore
Vespid waspOP
Yeah, I'll probably just deploy myself. Thank you so much though, this cleared up a lot