Workers, Pages Functions & 10ms CPU Time

Hi, Studying what could fit in the well-known "10ms CPU Time", I was surprised by some "discrepancies": - the same code in a worker is going to be (statistically) slightly faster than in a page-function, - that same function - if the only one - is going to be (statistically) slightly faster than grouped with others even non used. Of course, it's never much, but enough to make it jump over the 10ms! Is that logical, even expected? Anyone has done some real extensive tests?
5 Replies
zegevlier
zegevlier•2y ago
Are you seeing this in the real world, or are you just speculating?
TigersWay
TigersWay•2y ago
@zegevlier Real world 😃 But I have to agree with you it was a surprise! I will try to reproduce it, or them more accurately, And so for now, I'm just wondering how it could be!
zegevlier
zegevlier•2y ago
I'd also like to know how much time the normal execution is taking? How close are you to 10ms CPU time normally?
TigersWay
TigersWay•2y ago
@zegevlier @skye_31 I could not exactly share my code (too many work-in-progress, D1 tries and so on) but I built simple examples https://worker-native-typescript.tigersway.workers.dev/ & https://pages-native-typescript.pages.dev/count/
TigersWay
TigersWay•2y ago
Basically the same code, and it goes from 0.5ms to 3.1ms
export interface Env { }

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
if (request.method.toUpperCase() === 'POST') {
const
colo = request.cf?.colo || '',
contentType = request.headers.get('content-type') || '';
const body = contentType.includes('application/x-www-form-urlencoded') ? await request.json() : {};
return new Response(JSON.stringify({
success: true,
body,
colo
}), { headers: { 'content-type': 'application/json' } });
} else return new Response('POST Only!');
}
};
export interface Env { }

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
if (request.method.toUpperCase() === 'POST') {
const
colo = request.cf?.colo || '',
contentType = request.headers.get('content-type') || '';
const body = contentType.includes('application/x-www-form-urlencoded') ? await request.json() : {};
return new Response(JSON.stringify({
success: true,
body,
colo
}), { headers: { 'content-type': 'application/json' } });
} else return new Response('POST Only!');
}
};
interface Env { }

export const onRequestPost: PagesFunction<Env> = async (context) => {
const
colo = context.request.cf?.colo || '',
contentType = context.request.headers.get('content-type') || '';
const body = contentType.includes('application/x-www-form-urlencoded') ? await context.request.json() : {};
return new Response(JSON.stringify({
success: true,
body,
colo
}), { headers: { 'content-type': 'application/json' } });
};
interface Env { }

export const onRequestPost: PagesFunction<Env> = async (context) => {
const
colo = context.request.cf?.colo || '',
contentType = context.request.headers.get('content-type') || '';
const body = contentType.includes('application/x-www-form-urlencoded') ? await context.request.json() : {};
return new Response(JSON.stringify({
success: true,
body,
colo
}), { headers: { 'content-type': 'application/json' } });
};
I'm glad we finally understand each other 😄 But sad I then have no way to regain these little ms I needed. I'll be working on another architecture/organization. Thanks anyway Yep. It's on my list of things to try I never tried yet the worker site style... But yes, here are next working hours
Want results from more Discord servers?
Add your server