Does the first fetch request get 100ms runtime?

Hi,

can anyone confirm if the first fetch request gets 100ms runtime?

I did this test, and from the result, it seems like the first fetch request (I’m guessing the instantiating fetch request) gets more runtime than later requests:

  1. worker code
function functionToBeTested(count){
  let a = 0;
  for (let i = count; i < 1000000; i++) 
    a += Math.sqrt(i);
  return a;
}

async function handleRequest(request) {
  for(let i = 0; i < 200; i++){
    const a = functionToBeTested(i);
    console.log(i, (50/(i+1)).toFixed(2) + 'ms', a);
  }
  return new Response('hello sunshine');
}

addEventListener('fetch', e => e.respondWith(handleRequest(e.request)));
  1. paste this worker code into a blank https://cloudflareworkers.com/.

  2. The first time the worker runs, I get 32 ops. The second, third, forth, etc… I get approx 16ops.

If I am right, thank you cloudflare)

Each worker has a startup-buffer, which means it can temporarily use more CPU-time than the allotted 50ms. However, do NOT rely on this in production, it will expire the CPU-time since this buffer is limited to short duration (As evident by your test!). Always do proper distributed load-testing before going into production.

The extra CPU-time is required to have some margin when initializing large scripts.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.