demo · v140
Fixed-Frame Binary Reader
A simulated binary stream emits chunks at random sizes. Two BYOB readers race side by side: one without min accumulates partial reads; one with min: 8 waits for complete headers before yielding, eliminating reassembly entirely.
Frame format:
MAGIC 2B
+
LENGTH 2B
+
PAYLOAD (N bytes)
Header is always 4 bytes. Payload length is encoded in bytes 2-3. Stream emits chunks at random sizes: 3, 7, 2, 5, 12, 1, 9, …
Header is always 4 bytes. Payload length is encoded in bytes 2-3. Stream emits chunks at random sizes: 3, 7, 2, 5, 12, 1, 9, …
Without min (standard BYOB)
—total reads
—partial reads
—frames decoded
With min: 4 (header) then min: payload
—total reads
—partial reads
—frames decoded
// With min: the read resolves only when at least min bytes are filled
const reader = stream.getReader({ mode: "byob" });
// Read exactly 4 bytes for the header
const { value: header } = await reader.read(new Uint8Array(4), { min: 4 });
const magic = (header[0] << 8) | header[1]; // 0xDEAD
const len = (header[2] << 8) | header[3];
// Read exactly len bytes for the payload — zero reassembly
const { value: payload } = await reader.read(new Uint8Array(len), { min: len });
see also
- ReadableStream BYOB min option — feature index
- Protocol Decoder — sibling concept
- min vs default — read-count race
- ChromeStatus entry
- WHATWG Streams — BYOB read