Glossary / ArrayBuffer

ArrayBuffer

An ArrayBuffer is a raw region of binary memory. It does not define whether bytes mean integers, text, pixels, or floating-point values; typed-array views and DataView interpret the storage. ArrayBuffers are foundational for binary file/network APIs, workers, graphics, audio, and WebAssembly.

Storage and views

const buffer = new ArrayBuffer(16);
const bytes = new Uint8Array(buffer);
const words = new Uint32Array(buffer, 4, 2);

byteLength describes the buffer’s current size. Views specify byte offsets and lengths, and multiple views can share underlying bytes. Indexing through one view changes what another view observes. Resizable ArrayBuffers are supported in modern runtimes but require compatibility checks; fixed buffers remain common.

Structured cloning can copy an ArrayBuffer, while APIs with a transfer list (such as postMessage) can transfer ownership and detach the sender’s buffer, depending on the API. SharedArrayBuffer is a separate shared-memory capability; concurrent access requires synchronization with Atomics and browser security/isolation requirements. Do not assume transfer, sharing, and copying are interchangeable.

Binary-data guidance

Check offsets and byte lengths before constructing views or reading untrusted inputs. Define byte order in the protocol and use DataView for explicit control. Avoid exposing mutable views across trust boundaries if callers should not alter the underlying bytes.

Interview questions

  1. Does ArrayBuffer know the data type of its contents? No; a view supplies interpretation.
  2. What is the difference between ArrayBuffer and SharedArrayBuffer? One is normally exclusive memory; the other can be shared concurrently with synchronization rules.
  3. What can transfer do? Move ownership and detach the sender’s buffer in supported APIs.
  4. When use DataView? Heterogeneous fields and explicit endianness/offset handling.

References