Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

ArrayBuffer

BaselineWidely available *

TheArrayBuffer object is used to represent a generic raw binary data buffer.

It is an array of bytes, often referred to in other languages as a "byte array". You cannot directly manipulate the contents of anArrayBuffer; instead, you create one of thetyped array objects or aDataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.

TheArrayBuffer() constructor creates a newArrayBuffer of the given length in bytes. You can also get an array buffer from existing data, for example, from aBase64 string orfrom a local file.

ArrayBuffer is atransferable object.

Description

Resizing ArrayBuffers

ArrayBuffer objects can be made resizable by including themaxByteLength option when calling theArrayBuffer() constructor. You can query whether anArrayBuffer is resizable and what its maximum size is by accessing itsresizable andmaxByteLength properties, respectively. You can assign a new size to a resizableArrayBuffer with aresize() call. New bytes are initialized to 0.

These features make resizingArrayBuffers more efficient — otherwise, you have to make a copy of the buffer with a new size. It also gives JavaScript parity with WebAssembly in this regard (Wasm linear memory can be resized withWebAssembly.Memory.prototype.grow()).

Transferring ArrayBuffers

ArrayBuffer objects can be transferred between different execution contexts, likeWeb Workers orService Workers, using thestructured clone algorithm. This is done by passing theArrayBuffer as atransferable object in a call toWorker.postMessage() orServiceWorker.postMessage(). In pure JavaScript, you can also transfer the ownership of memory from oneArrayBuffer to another using itstransfer() ortransferToFixedLength() method.

When anArrayBuffer is transferred, its original copy becomesdetached — this means it is no longer usable. At any moment, there will only be one copy of theArrayBuffer that actually has access to the underlying memory. Detached buffers have the following behaviors:

  • byteLength becomes 0 (in both the buffer and the associated typed array views).
  • Methods, such asresize() andslice(), throw aTypeError when invoked. The associated typed array views' methods also throw aTypeError.

You can check whether anArrayBuffer is detached by itsdetached property.

Constructor

ArrayBuffer()

Creates a newArrayBuffer object.

Static properties

ArrayBuffer[Symbol.species]

The constructor function that is used to create derived objects.

Static methods

ArrayBuffer.isView()

Returnstrue ifarg is one of the ArrayBuffer views, such astyped array objects or aDataView. Returnsfalse otherwise.

Instance properties

These properties are defined onArrayBuffer.prototype and shared by allArrayBuffer instances.

ArrayBuffer.prototype.byteLength

The size, in bytes, of theArrayBuffer. This is established when the array is constructed and can only be changed using theArrayBuffer.prototype.resize() method if theArrayBuffer is resizable.

ArrayBuffer.prototype.constructor

The constructor function that created the instance object. ForArrayBuffer instances, the initial value is theArrayBuffer constructor.

ArrayBuffer.prototype.detached

Read-only. Returnstrue if theArrayBuffer has been detached (transferred), orfalse if not.

ArrayBuffer.prototype.maxByteLength

The read-only maximum length, in bytes, that theArrayBuffer can be resized to. This is established when the array is constructed and cannot be changed.

ArrayBuffer.prototype.resizable

Read-only. Returnstrue if theArrayBuffer can be resized, orfalse if not.

ArrayBuffer.prototype[Symbol.toStringTag]

The initial value of the[Symbol.toStringTag] property is the string"ArrayBuffer". This property is used inObject.prototype.toString().

Instance methods

ArrayBuffer.prototype.resize()

Resizes theArrayBuffer to the specified size, in bytes.

ArrayBuffer.prototype.slice()

Returns a newArrayBuffer whose contents are a copy of thisArrayBuffer's bytes frombegin (inclusive) up toend (exclusive). If eitherbegin orend is negative, it refers to an index from the end of the array, as opposed to from the beginning.

ArrayBuffer.prototype.transfer()

Creates a newArrayBuffer with the same byte content as this buffer, then detaches this buffer.

ArrayBuffer.prototype.transferToFixedLength()

Creates a new non-resizableArrayBuffer with the same byte content as this buffer, then detaches this buffer.

Examples

Creating an ArrayBuffer

In this example, we create a 8-byte buffer with aInt32Array view referring to the buffer:

js
const buffer = new ArrayBuffer(8);const view = new Int32Array(buffer);

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-arraybuffer-objects

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp