#Cold starts on Edge?

11 messages · Page 1 of 1 (latest)

lament finch
#

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.

jolly axleBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
golden sigil
#

That's the local dev server which behaves entirely different that the production workers Vercel uses.

lament finch
#

I thought it could be that, so if I deploy it won't have any cold starts?

golden sigil
#

Correct.

lament finch
#

Ahhh, okay. Thank you so much 🙏

golden sigil
#

Locally you have a debug build, a dev server that often recompiles on changes and runs everything under Node.

lament finch
#

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?

golden sigil
#

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

lament finch
#

Yeah, I'll probably just deploy myself. Thank you so much though, this cleared up a lot