CKTIS / JavaScript
ArrayBuffer: a raw byte buffer
ArrayBuffer reserves zero-filled byte memory that is accessed through a typed array or DataView and can optionally be resizable.
Version: ECMAScript 2024 onward for resizable and transferable ArrayBuffer; example target: Node.js 24.22.2
Syntax
new ArrayBuffer(byteLength[, { maxByteLength }])new ArrayBuffer(byteLength, options?) creates a zero-filled memory block. An ArrayBuffer itself does not expose indexed bytes; reading and writing require a view such as Uint8Array or DataView. byteLength goes through ToIndex: a finite fractional part is truncated toward zero, NaN and undefined become 0, and even -0.5 becomes -0 and therefore length 0. Calling the constructor without new throws TypeError.
options.maxByteLength requests a resizable buffer when the limit is not below the initial size. resize(newLength) preserves the common prefix, discards bytes when shrinking, and adds zero bytes when growing; length-tracking views adjust. A fixed buffer cannot be resized. slice() copies a selected range, while transfer operations can detach the source buffer.
Only a negative result after ToIntegerOrInfinity, +Infinity or -Infinity, exceeding the numeric range or the implementation’s practical memory limit, or a maximum below the initial size throws RangeError. Data-dependent operations on a detached buffer and resize() on a fixed buffer throw TypeError. Argument-conversion failures propagate. Resizable ArrayBuffer and transfer operations were standardized in ECMAScript 2024.
The scope label ArrayBuffer.byteLength denotes the instance property used as buffer.byteLength, not a static property of the ArrayBuffer constructor. This read-only accessor returns the current byte count: its result changes after resize() on a resizable buffer and is 0 after detachment. It has no setter; applying the getter to an incompatible receiver, including a SharedArrayBuffer, throws TypeError.
Examples
const buffer = new ArrayBuffer(4, { maxByteLength: 8 });
const bytes = new Uint8Array(buffer);
bytes.set([10, 20, 30, 40]);
console.log(buffer.byteLength, buffer.maxByteLength, buffer.resizable);
buffer.resize(6);
console.log(buffer.byteLength, Array.from(new Uint8Array(buffer)).join(","));
const copy = buffer.slice(1, 4);
console.log(copy.byteLength, Array.from(new Uint8Array(copy)).join(","));
try { new ArrayBuffer(4).resize(2); } catch (error) { console.log(error instanceof TypeError); }Result
4 8 true 6 10,20,30,40,0,0 3 20,30,40 true