Reducing / Debugging CPU time

I am dealing with an issue where in some circumstances, the requests take longer than the 50ms limit.

As it’s a rather big application, I am not sure where to start.

How do you guys trace/profile/debug your Workers execution time?

There’s no realistic way of doing that currently, you’ll have to break it down part by part until you find it.

Try to narrow it down to areas that do parsing, if you have loops that transform data, try streaming using different approaches that’s less CPU-consuming. You can time this in any browser console to see how fast the function processes the data - especially make sure you’re not recursively processing arrays of data, it’s better to create a reduce/generator function that matches content in multiple dimensions in that case.

If you’re loading large bodies of XML/JSON and don’t need to process it, then stream the response directly to where it needs to go. Even parsing a string of JSON that’s >1.5MB size into a variable would exceed the CPU-time.

1 Like

Thanks for your reply @thomas4.

OK. I am still a bit confused. I am deploying a static site to Cloudflare Workers. I assume I should debug the Workers Script (index.js file where I handle incoming requests) and not the actual application code I am deploying - right? I mean, the code I deploy is already “compiled” and should only use CPU on the client’s browser.

Yes, only the workers code. The site files is just handled as static assets and should not consume enough CPU time up be a problem.

1 Like

Thank you so much for your time Thomas.