subarray()
A typed array method that returns a new view onto part of the same memory, without copying any bytes.
In more detail
Changing the view changes the original, and holding the view keeps the whole original alive. Use slice() when you need an independent copy.
Example
Node.js Buffer.subarray() works the same way; Buffer's older slice() also shares memory and is deprecated, so use buf.copy() for a real copy.Related terms
- zero-copy — Handing data along by reference to the same memory rather than duplicating it, which saves time and memory.
- Buffer — A Node.js object holding a fixed-length run of raw bytes stored outside the V8 heap; more generally, any memory that holds data on its way somewhere.
- TypedArray — An array-like view, such as Uint8Array, that reads and writes numbers of one fixed type in an ArrayBuffer.
Taught in
- Buffer Encodings: UTF-8, Hex, Base64 & Slicing — Node.js Internals & Backend Services