Standalone (No WASM Module)
Overview
Section titled “Overview”You don’t need a WASM module to use zerobuf. Create a WebAssembly.Memory directly and use it as a structured binary buffer. This works in any JS runtime.
Pattern
Section titled “Pattern”import { zerobuf } from "zerobuf";
// No WASM module — just a memory bufferconst memory = new WebAssembly.Memory({ initial: 1 });const buf = zerobuf(memory);
// Structured data, stored in binaryconst config = buf.create({ region: "us-east-1", maxRetries: 3, endpoints: ["a.example.com", "b.example.com"],});
// Reads/writes go directly to the bufferconfig.maxRetries = 5;console.log(config.region); // "us-east-1"
// Nested objects and arrays workconfig.endpoints.push("c.example.com");When to Use This
Section titled “When to Use This”- Shared memory between Workers:
WebAssembly.Memorycan be shared across Web Workers viapostMessage(withSharedArrayBufferbacking). Both workers read/write the same structured data without serialization. - Binary serialization: use
.toJS()on one side,buf.create()on the other — or share the rawmemory.bufferbytes. - Structured arena allocation: append-only bump allocator with handle indirection. No GC pressure for short-lived objects.
Sharing Between Workers
Section titled “Sharing Between Workers”// Main threadconst memory = new WebAssembly.Memory({ initial: 1, maximum: 100, shared: true });const buf = zerobuf(memory);const data = buf.create({ counter: 0 });
worker.postMessage({ memory, ptr: data.__zerobuf_ptr });
// Worker threadonmessage = (e) => { const buf = zerobuf(e.data.memory); const data = buf.wrapObject(e.data.ptr); data.counter = 42; // main thread sees this immediately};Note: concurrent writes from multiple workers need external synchronization (e.g. Atomics). zerobuf does not provide locks.