Cloudflare has added the ability to run on‑demand CPU and heap profiling against Worker Previews and Durable Object previews. By sending test traffic to a preview, invoking the profiling API, and retrieving a flamegraph, you can see which functions in a pull‑request consume the most resources without touching live traffic.
How Preview Profiling Works
Each preview deployment receives a unique identifier. When you request a profile, the platform records execution data for the latest preview of the specified worker. The result is a flamegraph that breaks down self‑time and memory allocation per function, allowing you to pinpoint hotspots such as a repeatedly instantiated Intl.NumberFormat object.
Practical Steps for Engineers
Typical workflow:
- Deploy a preview locally with
npx wrangler previewor via CI. - Generate steady load against the preview URL.
- Capture a 10‑second CPU or heap profile using the Cloudflare CLI:
cf previews deployments profile latest \
--worker my-worker
The command returns a profile linked to the preview’s deployment ID. Review the flamegraph, modify the hottest function, and repeat until the self‑time no longer drops.
Architectural and Operational Implications
Because profiling occurs entirely within the preview environment, production latency and cost remain unchanged. This separation encourages a performance‑first mindset in code review pipelines and enables automated agents to iterate on optimisation without risking live traffic. The version‑specific IDs also make it possible to store profiling artifacts alongside build artefacts for audit or regression tracking.
From an operations perspective, teams can treat preview profiling as a lightweight, on‑demand alternative to full‑scale production observability. It reduces the need for long‑running synthetic load tests in production and can be integrated into pull‑request checks to enforce resource budgets.
Related CloudNinjas coverage: hands-on guides.
What This Means For Practitioners
Engineers should add a profiling step to their CI/CD flow for any worker that handles non‑trivial computation. Capture the deployment ID, record the top‑time functions, and enforce a threshold that triggers a review. Security‑focused teams can also use the isolation of previews to verify that performance tweaks do not unintentionally expose new attack surfaces, since the preview never touches production data. In short, preview profiling offers a low‑risk, high‑visibility feedback loop for CPU and memory optimisation before code lands in production.
