Examples
These examples show how to use Knitting in an application. Most include an optional benchmark comparing host and worker execution. For a detailed look at server performance, start with the Hono server example.
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 combines SSR, JWT, and health check routes in one HTTP server.
“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 transform and minify code” Bundling and minifying shows esbuild followed by Terser on workers, with a cap for async tasks that resume with CPU-heavy work.
“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), Proof of work (signal-cancelled hash search), and TSP (NP-hard optimization with parallel restarts).
“I need to index or ingest a pile of files” LaTeX paper indexing parses real arXiv LaTeX sources on workers, and measures where a decompress-then-parse job should be split.
“I need to generate documents” PDF invoices renders PDFs on workers, and shows how to preserve the binary type when returning data from a task.
“I just want to convert some text” Markdown to HTML is the simplest rendering example — Markdown in, compressed bytes out.
All examples
Section titled “All examples”Math and simulation
Section titled “Math and simulation”- Big prime — long-running BigInt search with Miller-Rabin
- Proof of work — find a SHA-256 hash with leading zeroes
- 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
Validation
Section titled “Validation”- Schema validation — Zod validation on workers
- JWT revalidation — HMAC verify + renewal with Web Crypto
- Salt hashing — PBKDF2 password hashing (heaviest per-call workload)
Rendering
Section titled “Rendering”- React SSR — render React components on workers
- React SSR compression — SSR + Brotli, worker vs host comparison
- Markdown to HTML — simple markdown transform pipeline
- PDF invoices — generate PDF documents and return the bytes
Build tools
Section titled “Build tools”- Bundling and minifying — transform TypeScript with esbuild, then minify with Terser on workers
- Prompt token budgeting — trim LLM prompts to a token budget
- LaTeX paper indexing — parse real arXiv sources into indexable prose
Servers
Section titled “Servers”- Hono server routes — real HTTP server with offloaded routes
Patterns worth copying
Section titled “Patterns worth copying”- Keep task modules separate from server code. Workers load each task module and its imports.
- Use concurrent calls for independent work.
Promise.allwaits for those calls together. To reduce dispatch overhead, group several items into one task input, as the PDF example does. - Return the data your application needs. A count or summary saves transfer work when the full result is unnecessary. The LaTeX example returns extracted text because indexing needs it; that transfer added no measurable time in its benchmark.
- Check output as well as speed. Compare content or hashes when correctness requires identical results; matching lengths alone is insufficient.
- Measure against direct execution. Try different worker counts and chunk sizes with the same input, and compare throughput, latency, and memory use.