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.
const buf = Buffer.from("abcdef"); const view = buf.subarray(1, 4); // "bcd", same memory
Try it
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.
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
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
endis not greater than the resolvedstart, 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
ArrayBufferfrom being garbage collected. Copy withBuffer.from(view)when you keep the piece long after the original is needed. - Pooled memory: small buffers made with
Buffer.from()andBuffer.allocUnsafe()may live in a shared pool, soview.buffercan be larger than the view andbyteOffsetis not zero. Never assumeview.buffercontains only your bytes. - Do not mix up the types:
Buffer.prototype.slicereturns a view, butUint8Array.prototype.slicereturns a copy.subarray()is a view on both, so code that uses it behaves the same everywhere. - Use
buf.copy()orBuffer.from(view)for ownership. Seebuf.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.