TypedArrays and Buffers

TypedArrays, ArrayBuffers and Interoperability

Because Buffer extends Uint8Array, it passes straight into Web Crypto, worker postMessage, WebAssembly memory and any browser-shaped API. The other direction needs care: an ArrayBuffer is raw storage and a typed array is a {buffer, byteOffset, length} window onto it, so a pooled buffer's .buffer is the slab.

Views, offsets and shared memoryJavaScript
const small = Buffer.from([1, 2, 3]); console.log(small.byteOffset, small.buffer.byteLength);
console.log('wrong:', Buffer.from(small.buffer).length);
console.log('right:', Buffer.from(small.buffer, small.byteOffset, small.length).length);
const ab = new ArrayBuffer(8);
const f64 = new Float64Array(ab); f64[0] = 1.5;
const shared = Buffer.from(ab);      // no copy: same bytes
console.log(shared.toString('hex')); shared[7] = 0x40;   // write through the view
console.log(f64[0], new DataView(ab).getUint32(4, false).toString(16));
Output
544 8192
wrong: 8192
right: 3
000000000000f83f
98304 f840

The first number is where this run placed three bytes in a pool slab; it differs every run. The second is the slab's size — and what Buffer.from(small.buffer) handed back.

Buffer.from(arrayBuffer) shares memory; Buffer.from(typedArray) copies the values, truncating each to one byte; Buffer.copyBytesFrom(typedArray) copies the underlying bytes, which is what a Uint16Array or Float32Array needs. DataView is the escape hatch when a format mixes widths and endianness in one record.

structuredClone(buf, { transfer: [buf.buffer] }) moves bytes to a worker without copying, detaching the original. Processes and Clustering covers SharedArrayBuffer.