Glossary / Typed arrays

Typed arrays

Typed arrays are fixed-length, array-like views over binary memory with a specific numeric element type. They support efficient numeric data for graphics, audio, files, network protocols, and WebAssembly. Unlike ordinary arrays, typed arrays do not contain holes and coerce values to their element representation, which can truncate, wrap, clamp, or round depending on the type.

Views and shared storage

Common types include Uint8Array, Int16Array, Float32Array, and BigInt variants. A typed array view has a buffer, byte offset, element length, and bytes-per-element. Multiple views can refer to the same ArrayBuffer and observe each other’s writes.

const buffer = new ArrayBuffer(8);
const bytes = new Uint8Array(buffer);
const firstFour = bytes.subarray(0, 4); // shared memory
const copy = bytes.slice(0, 4);        // copied values

subarray() creates a view; slice() creates a new typed array with copied elements. Typed arrays use the platform’s native byte order for multibyte element interpretation. For portable binary formats where endianness is specified, use DataView and pass the endianness explicitly. Resizable buffers and newer typed-array methods have varying runtime support; check your target matrix.

Input safety and performance

Validate buffer lengths and ranges before parsing untrusted binary data. Be explicit about integer overflow/wrapping and floating-point precision. Typed arrays are not automatically secure or faster for every workload; measure allocation, transfer, and conversion costs.

Interview questions

  1. Does subarray copy bytes? No; it returns a view over the same buffer.
  2. Does slice share storage? No; it copies elements.
  3. How handle protocol endianness? Use DataView with explicit littleEndian argument.
  4. Can typed arrays have holes? No, they are dense fixed-length numeric views.

References