Examples
These examples are real patterns you can copy into your project — not toy snippets. Most pages also point to an optional host-vs-worker benchmark script, but the example code is the main thing to copy. The Hono server page is the one that keeps the fuller performance story.
Pick the right example for your use case
Section titled “Pick the right example for your use case”“I have a web server and want to keep it responsive under load” Start with Hono server routes. It’s the most realistic example — a real HTTP server with SSR, JWT, and health check routes. If your server handles file uploads, Image processing API shows the same pattern with binary payloads (resize, thumbnail, watermark via sharp).
“I do SSR and want to offload rendering” React SSR shows the basic pattern. React SSR compression adds Brotli and tests where compression should live (worker vs host).
“I need to validate or parse lots of data” Schema validation (Zod), JWT revalidation (Web Crypto), or Salt hashing (PBKDF2) — pick whichever is closest to your workload.
“I’m building an LLM-powered app” Prompt token budgeting trims prompts to fit a token budget before they hit the API.
“I have a CPU-heavy computation I want to parallelize” The math examples cover the spectrum: Monte Carlo pi (embarrassingly parallel), Physics loop (variable-work simulation), Big prime (long-running search), and TSP (NP-hard optimization with parallel restarts).
“I just want to convert some text” Markdown to HTML is the simplest rendering example — string in, compressed string out.
All examples
Section titled “All examples”Math and simulation
Section titled “Math and simulation”- Big prime — long-running BigInt search with Miller-Rabin
- Monte Carlo pi — independent sampling and reduction
- Physics loop — branch-heavy simulation with variable work per trial
- TSP (GSA) — parallel heuristic restarts for NP-hard optimization
Data transforms
Section titled “Data transforms”- Data transforms overview — grouped by validation and rendering
- Schema validation — Zod validation on workers
- JWT revalidation — HMAC verify + renewal with Web Crypto
- Salt hashing — PBKDF2 password hashing (heaviest per-call workload)
- Prompt token budgeting — trim LLM prompts to a token budget
- React SSR — render React components on workers
- React SSR compression — SSR + Brotli, worker vs host comparison
- Hono server routes — real HTTP server with offloaded routes
- Image processing API — resize, thumbnail, and watermark with sharp on workers
- Markdown to HTML — simple markdown transform pipeline
Patterns worth copying
Section titled “Patterns worth copying”- Keep worker tasks focused and deterministic. One task, one job, predictable output.
- Batch calls and await them together (
Promise.all) to reduce scheduling overhead. - Return compact summaries from workers instead of large raw outputs.
- Validate on the host. Recompute critical metrics when needed — don’t trust worker output blindly.
- Compare against a host-only baseline before claiming speedups.
- Tune chunk sizes based on throughput — there’s always a sweet spot between dispatch overhead and load balance.