Node.js Reference ยท Buffers and Encoding
ArrayBuffer and SharedArrayBuffer in Node.js
Reference for ArrayBuffer and SharedArrayBuffer in Node.js: views, Buffer sharing, resizable buffers, transfer(), detached state and Atomics.
An ArrayBuffer is a fixed block of raw bytes. It has no way to read or write those bytes by itself: you look at it through a view, such as a Uint8Array, a Float64Array or a DataView. Several views can sit on the same ArrayBuffer, and they all see the same memory.
Node's Buffer is a Uint8Array subclass, so every Buffer sits on top of an ArrayBuffer too. A SharedArrayBuffer is the same idea, except that the memory can be handed to worker threads without copying, and the threads coordinate through Atomics.
const raw = new ArrayBuffer(16); const bytes = new Uint8Array(raw);
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.
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
new ArrayBuffer(byteLength, { maxByteLength? }) new SharedArrayBuffer(byteLength, { maxByteLength? }) buffer.byteLength // number buffer.maxByteLength // number buffer.resizable // boolean (ArrayBuffer) buffer.growable // boolean (SharedArrayBuffer) buffer.detached // boolean (ArrayBuffer) buffer.slice(begin?, end?) // copy buffer.resize(newByteLength) // ArrayBuffer buffer.grow(newByteLength) // SharedArrayBuffer buffer.transfer(newByteLength?) buffer.transferToFixedLength(newByteLength?) ArrayBuffer.isView(value)
| Name | Description |
|---|---|
byteLength |
Size in bytes. Becomes 0 after an ArrayBuffer is detached. |
maxByteLength |
Upper limit for a resizable or growable buffer. Equals byteLength for a fixed one. |
slice() |
Returns a new buffer holding a copy of the range. Unlike subarray() on a typed array, it does not share memory. |
resize() |
Changes the size in place, up to maxByteLength. The memory is kept; new bytes are zero. |
grow() |
The SharedArrayBuffer version of resize(). It can only get bigger. |
transfer() |
Moves the contents to a new ArrayBuffer and detaches the original. The new one keeps the resizable flag. |
transferToFixedLength() |
The same, but the result is never resizable. |
Errors: a negative or enormous length throws RangeError. Resizing beyond maxByteLength throws RangeError; calling resize() or transfer() on a detached buffer throws TypeError.
Usage notes
Buffershares memory by design.Buffer.from(arrayBuffer)andBuffer.from(arrayBuffer, byteOffset, length)are views, not copies. Writes through one are visible through the other.- The
Bufferpool. Small buffers made withBuffer.from(string)orBuffer.allocUnsafe()are carved from one shared 8 KiBArrayBuffer. If you need the exact bytes of aBufferas anArrayBuffer, usebuf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length), notbuf.buffer. - Detached buffers are empty. After
transfer()(or after a buffer is moved to a worker in apostMessagetransfer list), the old object reportsbyteLength0 and every view on it has length 0. - Length-tracking views. A typed array created without an explicit length on a resizable buffer follows the buffer's size. One created with a fixed length can become out of bounds if the buffer shrinks.
- Cloning.
structuredClone()andpostMessage()copy anArrayBufferunless it is in the transfer list, but aSharedArrayBufferis never copied: all sides share one block of memory. - Races. Plain reads and writes on a
SharedArrayBufferbetween threads can interleave in surprising ways. UseAtomicsfor counters, flags and locks. - Not a text container. To turn bytes into text use
buf.toString()orTextDecoder, not theArrayBufferitself. resize()andtransfer()need a current LTS release; both are available in Node.js 24.