The ArrayBuffer()
constructor creates ArrayBuffer
objects.
On this page
ArrayBuffer() constructor
Try it
Syntax
Parameters
-
length
-
The size, in bytes, of the array buffer to create.
options
Optional-
An object, which can contain the following properties:
maxByteLength
Optional-
The maximum size, in bytes, that the array buffer can be resized to.
Return value
A new ArrayBuffer
object of the specified size, with its maxByteLength
property set to the specified maxByteLength
if one was specified. Its contents are initialized to 0.
Exceptions
-
RangeError
-
Thrown in one of the following cases:
length
ormaxByteLength
is larger thanNumber.MAX_SAFE_INTEGER
(≥ 253) or negative.length
is larger thanmaxByteLength
.
Examples
Creating an ArrayBuffer
In this example, we create a 8-byte buffer with a Int32Array
view referring to the buffer:
const buffer = new ArrayBuffer(8);
const view = new Int32Array(buffer);
Creating a resizable ArrayBuffer
In this example, we create a 8-byte buffer that is resizable to a max length of 16 bytes, then resize()
it to 12 bytes:
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
buffer.resize(12);
Note: It is recommended that maxByteLength
is set to the smallest value possible for your use case. It should never exceed 1073741824
(1GB) to reduce the risk of out-of-memory errors.
Specifications
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
ArrayBuffer |
7 | 12 | 4 | 11.6 | 5.1 | 18 | 4 | 12 | 4.2 | 1.0 | 4 | 1.0 | 0.10.0 | |
maxByteLength_option |
111 | 111 | No | 97 | 16.4 | 111 | No | 75 | 16.4 | 22.0 | 111 | 1.33 | 20.0.0 | |
new_required |
7 | 14 | 44 | 15 | 5.1 | 18 | 44 | 14 | 5 | 1.0 | ≤37 | 1.0 | 0.12.0 |
See also
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/ArrayBuffer