Skip to content
Tutorials Center
My learning

Topics

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.

NODE
const raw = new ArrayBuffer(16);
const bytes = new Uint8Array(raw);

Try it

Node.js One block of memory, three views

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 Resizing and transferring

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 SharedArrayBuffer and Atomics

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
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

  • Buffer shares memory by design. Buffer.from(arrayBuffer) and Buffer.from(arrayBuffer, byteOffset, length) are views, not copies. Writes through one are visible through the other.
  • The Buffer pool. Small buffers made with Buffer.from(string) or Buffer.allocUnsafe() are carved from one shared 8 KiB ArrayBuffer. If you need the exact bytes of a Buffer as an ArrayBuffer, use buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length), not buf.buffer.
  • Detached buffers are empty. After transfer() (or after a buffer is moved to a worker in a postMessage transfer list), the old object reports byteLength 0 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() and postMessage() copy an ArrayBuffer unless it is in the transfer list, but a SharedArrayBuffer is never copied: all sides share one block of memory.
  • Races. Plain reads and writes on a SharedArrayBuffer between threads can interleave in surprising ways. Use Atomics for counters, flags and locks.
  • Not a text container. To turn bytes into text use buf.toString() or TextDecoder, not the ArrayBuffer itself.
  • resize() and transfer() need a current LTS release; both are available in Node.js 24.