Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Buffers and Encoding

Node.js buf.subarray() and buf.slice() methods

Reference for Node.js buf.subarray() and the deprecated buf.slice(): create a view onto the same memory, use negative indexes, and copy when you need your own bytes.

buf.subarray([start[, end]]) returns a new Buffer that looks at part of the original memory. No bytes are copied. Writing through the view changes the original buffer, and the other way round. This makes subarray() very cheap, which is why parsers use it to split packets into fields.

buf.slice() does the same thing on Buffer, but it is deprecated because Array.prototype.slice() and Uint8Array.prototype.slice() return copies, which made the name misleading. Use subarray() in new code.

NODE
const buf = Buffer.from("abcdef");
const view = buf.subarray(1, 4); // "bcd", same memory

Try it

Node.js A view shares memory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Negative indexes and copies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
buf.subarray(start = 0, end = buf.length)  // returns Buffer (a view)
buf.slice(start = 0, end = buf.length)     // deprecated alias, same behavior
Name Description
start Index of the first byte. A negative value counts from the end. Default 0.
end Index to stop at, not included. A negative value counts from the end. Default buf.length.

Returns: a Buffer whose buffer, byteOffset and length describe the selected range of the same ArrayBuffer.

Edge cases:

  • Indexes are clamped to the buffer; they never throw for being out of range.
  • If the resolved end is not greater than the resolved start, you get an empty buffer (still a view).
  • Non-integer indexes are truncated toward zero.

Usage notes

  • The view keeps the whole memory alive. A small subarray of a huge buffer prevents the huge ArrayBuffer from being garbage collected. Copy with Buffer.from(view) when you keep the piece long after the original is needed.
  • Pooled memory: small buffers made with Buffer.from() and Buffer.allocUnsafe() may live in a shared pool, so view.buffer can be larger than the view and byteOffset is not zero. Never assume view.buffer contains only your bytes.
  • Do not mix up the types: Buffer.prototype.slice returns a view, but Uint8Array.prototype.slice returns a copy. subarray() is a view on both, so code that uses it behaves the same everywhere.
  • Use buf.copy() or Buffer.from(view) for ownership. See buf.copy().
  • Handy for parsing: slice a header and a body off a packet with two subarray() calls and no copying.
  • Typed array sharing: new Uint8Array(buf.buffer, buf.byteOffset, buf.length) creates a view the same way, but you must pass the offset yourself.