Very slow fetch inside worker

I’m very new to CloudFlare, just created my first account.

I’m using workers to call a 3rd party API. The worker is hosted here: https://worker1.fp1.workers.dev/

Here is the worker code:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  let d1 = Date.now();
  let apiResponse = await fetch("https://api.fpjs.io");
  let diff = Date.now() - d1;
  return new Response(`took ${diff} ms to call the API`, {
    headers: { 'content-type': 'text/plain' },
  })
}

When I’m testing the worker in the built-in editor, the API call takes 31-33ms consistently (after first start).

However when I’m testing it in actual browser, the API call takes ~600ms consistently.

This time difference is unfortunately a deal breaker for me, 31-33ms is perfect, while 600ms is not acceptable.

Why is this happening? Can I make it call the API in 33ms like it’s doing in the editor? Is there a way to seed it up?

I’m testing from Chicago, the API is hosted on AWS (N. Virginia).

https://api.fpjs.io does take a bit to load.

2 Likes

Have you tried using the worker route in your domain with cloudflare?

From what I’m able to tell, when you’re testing using the editor, it’s not actually running the code on a Cloudflare server. Requests originate from Google IP addresses when testing via the editor, which means traffic will be taking a different path than it does once you’ve actually deployed it.

1 Like

yes, the timing didn’t change

This topic was automatically closed after 30 days. New replies are no longer allowed.