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.
Creating a buffer
Section titled “Creating a buffer”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); // 3The constructor takes an optional ArrayBuffer or array-like of numbers as the initial unread contents.
Reading consumes bytes
Section titled “Reading consumes bytes”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.
Writing appends bytes
Section titled “Writing appends bytes”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)); // 4console.log(await buf.read(rest)); // nullconsole.log(buf.empty()); // truebytes() 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.
Filling a buffer from a file
Section titled “Filling a buffer from a file”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.
When not to use it
Section titled “When not to use it”For most new code you don’t need Buffer at all:
- To read a whole file,
await Deno.readFile(path)returns aUint8Array. - To collect a
ReadableStream(a file’s.readable, afetchbody),await new Response(stream).bytes()does it in one line. - To join chunks you already have,
concatfrom@std/bytesis 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.
Further reading
Section titled “Further reading”@std/ioBuffer API docs- Go
bytes.Buffer, the design it copies - Streams in Deno
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)