how can I make my api call faster?
Answered
American Robin posted this in #help-forum
American RobinOP
I just realized, when I call the same api in localhost it needs 100ms~, when it’s deployed under domainname.vercel… it needs 300-330ms and when I call it over my own domain which I use it needs 400~ ms, how can I make it faster? Or why is it like that
Thanks for any answer!
Thanks for any answer!
128 Replies
American RobinOP
With my api call, I call some data from my own DB
With Prisma
American RobinOP
The db is not local
You will probably need to profile some stuff in your api, see which parts are taking how much in what environments.
The call to your Route Handler and its response can probably not be made any faster with just code changes.
You might have to optimize your code depending on what you have in your code. If its just a prisma call then maybe its just that the database calls and responses are taking too long.
The call to your Route Handler and its response can probably not be made any faster with just code changes.
You might have to optimize your code depending on what you have in your code. If its just a prisma call then maybe its just that the database calls and responses are taking too long.
@Clown You will probably need to profile some stuff in your api, see which parts are taking how much in what environments.
The call to your Route Handler and its response can probably not be made any faster with just code changes.
You might have to optimize your code depending on what you have in your code. If its just a prisma call then maybe its just that the database calls and responses are taking too long.
American RobinOP
in local it’s 100ms and deploy 300-400ms
I changed a lot so far
Removed unnecessary Prisma calls
American RobinOP
Could I maybe use the edge api? Is Prisma available there
@Clown You will probably need to profile some stuff in your api, see which parts are taking how much in what environments.
The call to your Route Handler and its response can probably not be made any faster with just code changes.
You might have to optimize your code depending on what you have in your code. If its just a prisma call then maybe its just that the database calls and responses are taking too long.
American RobinOP
oh i figured out smth, in local the content encoding is: gzip while on prod its on: br and when i check the answer there is no "security" tab on the network
American RobinOP
prod
local
i guess its because vercel
maybe thats why? it takes longer?
local
prod
So subsequent calls to vercel take as long?
Keep in mind there’s a cold start
For the first call
And if you’re communicating with dbs or external services
American RobinOP
ye its not the cold start
There’s latency
American RobinOP
i do 1-2 calls b4
to test it
@linesofcode And if you’re communicating with dbs or external services
American RobinOP
i do that in both ways
one is localhost one is prod
If you really need low latency responses
Maybe the edge runtime can help
Answer
American RobinOP
prod
local
If you turn some of those api routes to edge
American RobinOP
i looked about edge functions
is it hard to implement?
im using prisma
Depends
But I think prisma has support via data proxy
American RobinOP
ye
Before doing any of that
American RobinOP
saw some videos about that
Consider if you really need this
American RobinOP
its a guessing game
where the user sees the result
after guessing
Oh edge can be perfect for this
Or you can use pusher or ably
To do websockets for real time communication
American RobinOP
what is that?
Pretty commonly used along side a vercel app
If you need real time
But don’t want to manage the infrastructure yourself
American RobinOP
mhmmm
im doint some maths in the api call
But it totally depends on what you’re doing
Just an alternative to consider
American RobinOP
wait can i dm u
ah and btw, the call is 100ms faster when im not logged in on prod so 350 instead of 450 and 83ms instead of 100-110ms on local
cause there are some more things the api does when logged in
No I don’t do dms sorry
Do you use next-auth
American RobinOP
no i use clerk
Ah okay
American RobinOP
when the user is logged in and plays it gets to the leaderboard
etc
but i guess i should give edge a try right
@linesofcode https://pusher.com/
American RobinOP
or this
Edge first
American RobinOP
do i have to change alot?
Hopefully no
American RobinOP
i saw smth like i need a prisma smth acc
But if you’re using node only APIs
They may not work
American RobinOP
mhmm what u mean by that
American RobinOP
oke ill give it a try i guess
Edge has a limited set of APIs available
Otherwise everyone would be running on it
American RobinOP
prisma should work i guess
Yes with their edge specific configs, etc
American RobinOP
ye
what u think
how would the ms get
from 350-450ms
to 100-200?
or even less
American RobinOP
ye ill do that
thanks for ur help so far
American RobinOP
@linesofcode im currently tryin to use edge,
✔ Generated Prisma Client (v5.2.0, engine=none) to ./node_modules/@prisma/client in 79ms
Start using Prisma Client in Node.js (See: https://pris.ly/d/client)
or start using Prisma Client at the edge (See: https://pris.ly/d/accelerate)
does it load the normal prisma one?
prisma generate --data-proxyPrisma schema loaded from prisma/schema.prisma
✔ Generated Prisma Client (v5.2.0, engine=none) to ./node_modules/@prisma/client in 79ms
Start using Prisma Client in Node.js (See: https://pris.ly/d/client)
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()or start using Prisma Client at the edge (See: https://pris.ly/d/accelerate)
import { PrismaClient } from '@prisma/client/edge'
const prisma = new PrismaClient()does it load the normal prisma one?
✔ Generated Prisma Client (v5.2.0, engine=none) to ./node_modules/@prisma/client in 79ms
i guess its using the normal one right
i guess its using the normal one right
import { PrismaClient } from '@prisma/client/edge'Take a look at this
I dont use prisma btw
so the only advice I can give you is high-level
@linesofcode `import { PrismaClient } from '@prisma/client/edge'`
American RobinOP
i did this
i had to use this:
iin my package.json scripts:
"postinstall": "prisma generate --data-proxy"
iin my package.json scripts:
"postinstall": "prisma generate --data-proxy"
i added --data-proxy
but its no there
American RobinOP
@linesofcode
i got it workin
thanks alot
from 350ms to 100 when not logged in and 450ms to 150ms when logged in
American RobinOP
even in localhost it got so much faster
100ms -> 40-50ms
Awesome!
congrats
American RobinOP
@linesofcode quick question, when i use edge functions, do i have to make every api call and edge function calll?
can you rephrase that question please?
American RobinOP
i changed my api call to an edge api call right
as u mentioned
but do i have to change every call now to an edge function?
yeah
the ones you deem are necessary
to be turned into edge functions
should be turned into edge functions
at least thats how I approach the problem
idk if prisma data proxy has other opinions
American RobinOP
oh i see
currently i have 4 api calls i do change them all now to edge functions
If that’s what you need, then yes