Skip to content

SSR + compression

This example adds Brotli compression to the React SSR example. It compares rendering and compression on the worker with rendering on the worker and compression on the host. Keeping both steps together also reduces the amount of data returned to the host.

The host generates JSON payload strings, and the benchmark measures three placements of the same work:

  1. host — render and compress on the main thread.
  2. worker: render only, host compresses — the worker returns HTML, the host runs Brotli.
  3. worker: render + compress — both steps on the worker, so only the compressed bytes come back.

All three must produce identical compressed byte totals; the benchmark checks that before measuring.

  • bench_react_ssr_compress.ts — the benchmark
  • render_user_card_compressed.tsx — the SSR + compression task

Payloads and the card component come from the React SSR example, so the only new code here is the compression step.

Input is the same JSON payload shape as the plain React SSR example. The worker call is:

const compressed = await pool.call.renderUserCardCompressed(payloadJson);

Output is a Brotli-compressed Buffer, not an HTML string. That is the point of this example: do the expensive render and compression work in one place, then return the smaller payload.

Compression uses built-in node:zlib — no extra packages.

Measured with Knitting 0.1.77.

bun.sh
bun src/bench_react_ssr_compress.ts

Expected output (4 cores / 8 threads, Bun 1.4, 100 requests per iteration):

byte parity check: host=52,551 worker=52,551 split=52,551 OK match
React SSR + compression benchmark (mitata)
workload: parse + normalize + render + brotli
requests per iteration: 100
threads: 4 + inliner
benchmark avg (min … max)
host: render + compress 294.18 ms/iter
worker: render only, host compresses 284.79 ms/iter
worker: render + compress 111.20 ms/iter
summary
worker: render + compress
2.65x faster than host: render + compress
2.56x faster than worker: render only, host compresses

Rendering and compressing in workers took 111 ms per batch, compared with 294 ms on the host: a 2.65× speedup. Offloading rendering alone took 285 ms because the host still compressed every response and received the full HTML.

This test was faster with an inliner, which let the host share the CPU work. The plain SSR example was faster without one. Measure rendering and compression together when choosing the pool configuration.

render_user_card_compressed.tsx
import { task } from "knitting";
import { brotliCompressSync } from "node:zlib";
import { renderUserCardHost } from "../react_ssr/render_user_card.tsx";
export const compressHtml = (html: string): Uint8Array =>
brotliCompressSync(html);
/**
* Render the card to HTML and Brotli-compress it. Exported so the benchmark can
* run exactly the same work on the host instead of keeping a second copy of it.
*/
export function renderAndCompressHost(payloadJson: string): Uint8Array {
return compressHtml(renderUserCardHost(payloadJson));
}
export const renderUserCardCompressed = task({
f: renderAndCompressHost,
});

Compressing in the worker returns a small buffer instead of the full HTML. For an HTTP server, check request latency under load as well as batch throughput.