Note: The SharedArrayBuffer constructor may not always be globally available unless certain security requirements are met.
The SharedArrayBuffer() constructor creates SharedArrayBuffer objects.
Note: The SharedArrayBuffer constructor may not always be globally available unless certain security requirements are met.
The SharedArrayBuffer() constructor creates SharedArrayBuffer objects.
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 shared array buffer can be resized to.
A new SharedArrayBuffer object of the specified size, with its maxByteLength property set to the specified maxByteLength if one was specified. Its contents are initialized to 0.
SharedArrayBuffer constructors are required to be constructed with a new operator. Calling a SharedArrayBuffer constructor as a function without new will throw a TypeError.
const sab = SharedArrayBuffer(1024);
// TypeError: calling a builtin SharedArrayBuffer constructor
// without new is forbidden
const sab = new SharedArrayBuffer(1024);
In this example, we create an 8-byte buffer that is growable to a max length of 16 bytes, then grow() it to 12 bytes:
const buffer = new SharedArrayBuffer(8, { maxByteLength: 16 });
buffer.grow(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.
| 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 | ||
SharedArrayBuffer |
68 | 79 | 79 | 55 | 15.2 | 89 | 79 | 63 | 15.2 | 15.0 | No | 1.0 | 8.10.0 | |
maxByteLength_option |
111 | 111 | No | 97 | 16.4 | 111 | No | 75 | 16.4 | 22.0 | No | 1.33 | 20.0.0 | |
© 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/SharedArrayBuffer/SharedArrayBuffer