Skip to content

Byte buffers in Deno with @std/io Buffer

Buffer from @std/io is a growable byte queue modelled on Go’s bytes.Buffer. You write Uint8Array chunks onto the end and read them off the front; it grows its underlying ArrayBuffer as needed. It is not Node’s Buffer, and it isn’t a general-purpose binary data type: it’s for collecting bytes from a Reader (a file or socket) and handing them out again.

import { Buffer } from "jsr:@std/io@0.225/buffer";
const empty = new Buffer();
const buf = new Buffer(new Uint8Array([65, 66, 67])); // "ABC"
console.log(buf.length); // 3

The constructor takes an optional ArrayBuffer or array-like of numbers as the initial unread contents.

read(p) copies up to p.length unread bytes into p and returns how many it copied. Those bytes are then gone from the buffer: it’s a queue, not a random-access array. When the buffer is empty, read returns null (end of input), not 0.

const readData = new Uint8Array(2);
const n = await buf.read(readData);
console.log(n, readData); // 2 Uint8Array(2) [ 65, 66 ]
console.log(buf.length); // 1 ("C" is still unread)

readSync does the same thing synchronously. The async version exists so Buffer satisfies the Reader interface; there’s no actual I/O.

const written = await buf.write(new TextEncoder().encode("DEF"));
console.log(written); // 3
console.log(new TextDecoder().decode(buf.bytes({ copy: false }))); // "CDEF"
const rest = new Uint8Array(10);
console.log(await buf.read(rest)); // 4
console.log(await buf.read(rest)); // null
console.log(buf.empty()); // true

bytes() returns the unread portion without consuming it. With { copy: false } it returns a view into the buffer’s memory, which is only valid until the next read, write, reset or truncate; pass { copy: true } if you need to keep it.

There is no seek or slice method. To drop data, use truncate(n) (keep only the first n unread bytes) or reset() (empty the buffer but keep the allocation). grow(n) pre-allocates room for n more bytes if you know roughly how much is coming.

readFrom reads from any Reader until end of input. A Deno.FsFile still has a read method, so it works directly:

import { Buffer } from "jsr:@std/io@0.225/buffer";
using file = await Deno.open("data.bin");
const buf = new Buffer();
const n = await buf.readFrom(file);
console.log(`read ${n} bytes, ${buf.length} unread`);

Run it with deno run --allow-read. The using declaration closes the file when it goes out of scope.

For most new code you don’t need Buffer at all:

  • To read a whole file, await Deno.readFile(path) returns a Uint8Array.
  • To collect a ReadableStream (a file’s .readable, a fetch body), await new Response(stream).bytes() does it in one line.
  • To join chunks you already have, concat from @std/bytes is simpler.

Buffer is also an ordinary single-threaded object with no locking. Code within one isolate can’t race on it, but it can’t be shared with a Worker either: postMessage copies what you send, so send the bytes (buf.bytes({ copy: true })) rather than the Buffer. If you really need memory shared between workers, use a SharedArrayBuffer with Atomics.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)