cloudflare-workers
Optimize code for Cloudflare Workers free plan with 10ms CPU limit. Use when writing API routes, server code, middleware, or discussing performance optimization.
When & Why to Use This Skill
This Claude skill specializes in optimizing serverless applications for the Cloudflare Workers free plan, specifically focusing on the strict 10ms CPU time limit. It helps developers implement high-performance edge computing patterns such as response streaming, asynchronous execution, and efficient data handling. By identifying common bottlenecks and anti-patterns like synchronous operations or heavy JSON parsing, the skill ensures your API routes and middleware remain fast, scalable, and cost-effective within platform constraints.
Use Cases
- Refactoring API middleware to ensure request processing stays under the 10ms CPU limit.
- Implementing response streaming to reduce memory overhead and improve Time to First Byte (TTFB).
- Optimizing data fetching logic by replacing sequential awaits with parallel Promise execution.
- Identifying and moving heavy computations (like image processing or complex sorting) from the Worker to the client-side.
- Configuring efficient Cache-Control headers and CDN caching strategies to minimize execution frequency.
| name | cloudflare-workers |
|---|---|
| description | Optimize code for Cloudflare Workers free plan with 10ms CPU limit. Use when writing API routes, server code, middleware, or discussing performance optimization. |
Cloudflare Workers Optimization
Constraints
- 10ms CPU limit on free plan (not wall-clock time)
- No heavy computation in request handlers
- No blocking operations
Patterns
Prefer Streaming
// Good: Stream response
return new Response(readableStream, {
headers: { 'Content-Type': 'application/octet-stream' },
});
// Bad: Buffer entire response
const data = await fetchAllData();
return new Response(JSON.stringify(data));
Delegate to Client
Move these to browser when safe:
- Image manipulation (canvas API)
- Complex calculations
- Data transformations
- Sorting/filtering large datasets
Efficient Algorithms
- Use early returns
- Avoid nested loops on large datasets
- Use Map/Set for O(1) lookups
- Paginate database queries
Async Patterns
// Good: Parallel fetches
const [user, settings] = await Promise.all([getUser(id), getSettings(id)]);
// Bad: Sequential fetches
const user = await getUser(id);
const settings = await getSettings(id);
Response Caching
return new Response(body, {
headers: {
'Cache-Control': 'public, max-age=3600',
'CDN-Cache-Control': 'max-age=86400',
},
});
Anti-patterns
- Synchronous crypto operations
- Large JSON parsing in hot paths
- Regex on untrusted input without limits
- Deep object cloning