JavaScript Reference ยท Typed Arrays and Binary Data
The ArrayBuffer object
Reference for the JavaScript ArrayBuffer object: create raw binary buffers, read them through views, slice, resize, transfer and check for views, with live examples.
An ArrayBuffer is a fixed-size block of raw bytes. It has no way to read or write its own contents: you look at the bytes through a view, such as a typed array (Uint8Array, Float64Array, ...) or a DataView. Several views can share the same buffer, each interpreting the same bytes in a different way.
A new buffer is always filled with zeros. Buffers are what network APIs, files, canvases, WebAssembly and crypto hand you when they deal with binary data.
const buffer = new ArrayBuffer(8); // 8 bytes, all zero const bytes = new Uint8Array(buffer);
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
new ArrayBuffer(byteLength) new ArrayBuffer(byteLength, { maxByteLength }) buffer.byteLength buffer.slice(begin, end) buffer.resize(newByteLength) ArrayBuffer.isView(value)
| Name | Description |
|---|---|
byteLength |
Size in bytes. Converted to an integer; must not be negative or too large for the engine. |
maxByteLength |
Optional. Makes the buffer resizable, up to this many bytes. |
begin, end |
For slice(): byte positions. Negative values count from the end, as in Array slice(). |
newByteLength |
For resize(): the new size, at most maxByteLength. |
Members
byteLength: the current size in bytes.maxByteLength: the largest size a resizable buffer can reach (the same asbyteLengthfor a fixed buffer).resizable:trueif the buffer was created withmaxByteLength.slice(begin, end): returns a new buffer with a copy of those bytes.resize(n): grows or shrinks a resizable buffer in place.ArrayBuffer.isView(value):truefor typed arrays andDataView,falsefor buffers and everything else.transfer(newLength)andtransferToFixedLength(newLength): move the contents into a new buffer and detach the original. A detached buffer hasbyteLength0and itsdetachedproperty istrue.
Return value: the constructor returns a new buffer; slice() returns a new ArrayBuffer; resize() returns undefined.
Exceptions: new ArrayBuffer() without new throws TypeError. A negative or oversized length, or resizing beyond maxByteLength, throws RangeError.
Usage notes
- A buffer is not an array. You cannot index it; create
new Uint8Array(buffer)(or another view) first. - Views share memory. Writing through one view changes what every other view over the same buffer sees. Use
slice()on the buffer (or on a typed array) when you need an independent copy. - Byte order. Typed arrays use the byte order of the machine (little-endian on virtually all current devices). Use a
DataViewwhen the format fixes the order, as network protocols do. - Resizable buffers. A view created without an explicit length follows the buffer when it is resized. Views with a fixed length may go out of bounds after shrinking, which makes their length read as
0. - Transferring.
transfer()andpostMessage()with a transfer list move a buffer between owners without copying, which matters for large data. - Memory. Large buffers are allocated eagerly and zero-filled; a big request can fail with
RangeError.
Browser support
ArrayBuffer has been supported in all browsers for a long time. Resizable buffers and transfer() are newer additions, so check a compatibility table before relying on them for older browsers.