CrabTalk
Blog

serde_json vs postcard in WebAssembly: measured CPU and binary size

serde_json in Rust WebAssembly services, measured: up to 87% of a call's CPU at 500 KB and 35% of a small binary. Where it goes, and what postcard saves.

CrabTalk Team

Services on CrabTalk are Rust compiled to WebAssembly. Every tool call takes JSON arguments and returns a JSON result, and many services also parse JSON from the APIs they call. We bill a call by the CPU it uses, so we measured how much of that CPU, and of each binary, is serde_json, and whether a binary format like postcard would be worth switching to.

Short answer: on small calls JSON is noise. On large payloads it's most of the bill, but most of that comes from two avoidable patterns, not from JSON being slow. Postcard is 3–10× faster at decoding large arguments, and still not a drop-in replacement.

Method

  • Code: the public crab repo at commit 6209b62, measured on 2026-11-17.
  • Services: three real ones: firecrawl (generated from an OpenAPI document, returns the provider's JSON), page-watch (stores page snapshots as Markdown) and agent_memory (stores memories in SQLite).
  • Build: crab build, release, wasm32-unknown-unknown.
  • CPU: guest CPU as the host meters it for billing, measured with the same clock, via crab bench --offline on an Apple arm64 machine. Each case ran 41 times; we discarded the first and report medians of the remaining 40.
  • Payloads: 200 B, 10 KB and 500 KB per case. Firecrawl's provider responses are fixtures, so no network time is counted.
  • Binary size: Twiggy, summing the bytes of functions whose names mention serde_json. That undercounts inlined code, and doesn't predict what removing JSON would save.

Binary size

Service Wasm Gzipped JSON-related code Share
agent_memory 170,401 B 64,010 B 59,077 B 34.7%
firecrawl 462,465 B 159,479 B 82,834 B 17.9%
page-watch 2,029,208 B 725,271 B 90,263 B 4.5%

JSON costs roughly 60–90 KB of code whatever the service does, so it dominates small services and fades in large ones. serde_json monomorphizes its serializer and deserializer per type, so a service with many types pays more.

CPU per call

Medians in µs of metered guest CPU:

Call Payload JSON Whole call JSON share
firecrawl scrape 200 B response 2.8 30.0 9.3%
10 KB 50.2 127.5 39.6%
500 KB 1,019.5 1,199.0 86.8%
memory put 200 B 1.6 29.5 5.8%
10 KB 21.9 73.5 29.9%
page-watch compare 200 B 1.6 66.5 2.5%
10 KB 30.9 197.5 15.7%
500 KB 1,082.8 2,633.0 47.6%
page-watch save 200 B 4.0 124.0 3.2%
10 KB 24.5 92.0 26.1%
500 KB 2,144.0 2,503.5 84.3%

Two things stand out.

Small calls don't care. At 200 B, JSON is 1.6–4 µs, under a tenth of the call.

JSON can cost more than the database. For memory.put with 10 KB, JSON took 21.9 µs and SQLite took 20.0 µs of CPU. It's easy to assume a call's cost is its I/O, but waiting on a network or a disk isn't CPU, and parsing is.

Where the CPU actually goes

At 500 KB, two calls spend over 84% of their CPU on JSON. Neither is because JSON is slow; both do the work twice.

Parsing a response only to write it back. The firecrawl tool parses the provider's response into a serde_json::Value, returns it, and the result is serialized again: 752 µs of a 1,199 µs call spent re-encoding bytes the provider already sent. If a tool returns the provider's JSON unchanged, it doesn't need a tree at all. serde_json's RawValue (behind the raw_value feature) checks the bytes are valid JSON without building one, and serializes them as they are.

Encoding a string as JSON to store it. page-watch decodes 500 KB of Markdown from its arguments (783 µs), then stores it with a helper that encodes the same string as JSON again (quotes, escapes, all of it), and decodes it back on the next comparison. Storing a string as its UTF-8 bytes skips both.

We're applying both changes to our services. The point for anyone writing Rust for Wasm: look for a JSON round trip before blaming JSON.

Would postcard help?

We swapped only the argument and result codec for postcard, on the same Rust types, and measured just that step:

Call Payload serde_json postcard
memory put 10 KB 21.9 µs 6.6 µs
16 KB 73.6 µs 3.4 µs
500 KB 327.4 µs 125.1 µs
page-watch compare 200 B 1.2 µs 1.5 µs
10 KB 17.4 µs 5.4 µs
500 KB 487.0 µs 79.1 µs
page-watch save 10 KB 7.1 µs 2.8 µs
500 KB 784.3 µs 86.5 µs

Postcard is 3–10× faster on large payloads and about even on tiny ones. We're still not switching, for four reasons:

  1. Callers speak JSON. Agents call tools over MCP and HTTP, both JSON. A binary format inside the service means converting at the edge, which moves the cost rather than removing it.
  2. serde_json::Value doesn't work in postcard. Postcard isn't self-describing, so types that rely on deserialize_any (Value, untagged enums, flatten) can't round-trip. An OpenAPI-generated service is full of Value.
  3. Field order becomes the contract. Postcard encodes fields by position. Adding an optional field or reordering a struct (both safe in JSON) becomes a breaking change for every caller.
  4. The boundary is text. In our runtime, a tool's result crosses back to the host as a UTF-8 string, so binary results don't fit without changing it.

If a service's arguments are large and internal, and only your own code calls it, postcard's numbers are worth it. For tools that agents call, fix the double work first; on our measurements that recovers most of the cost.

Takeaways

  • Under about 1 KB, don't optimize JSON.
  • Over about 10 KB, measure: JSON can be a third or more of a call's CPU, ahead of database work.
  • Look for round trips first: parsing to re-serialize, or encoding a value only to store it.
  • Pass through JSON you don't inspect, with RawValue.
  • Store strings as bytes, not as JSON strings.
  • Postcard is much faster, but only where you own both ends and can treat field order as a contract.