Web/JavaScript/Reference/Global objects/SharedArrayBuffer

From Get docs


The SharedArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer, similar to the ArrayBuffer object, but in a way that they can be used to create views on shared memory. Unlike an ArrayBuffer, a SharedArrayBuffer cannot become detached.

Description

Allocating and sharing memory

To share memory using SharedArrayBuffer objects from one agent in the cluster to another (an agent is either the web page’s main program or one of its web workers), postMessage and structured cloning is used.

The structured clone algorithm accepts SharedArrayBuffers and TypedArrays mapped onto SharedArrayBuffers. In both cases, the SharedArrayBuffer object is transmitted to the receiver resulting in a new, private SharedArrayBuffer object in the receiving agent (just as for ArrayBuffer). However, the shared data block referenced by the two SharedArrayBuffer objects is the same data block, and a side effect to the block in one agent will eventually become visible in the other agent.

var sab = new SharedArrayBuffer(1024);
worker.postMessage(sab);

Updating and synchronizing shared memory with atomic operations

Shared memory can be created and updated simultaneously in workers or the main thread. Depending on the system (the CPU, the OS, the Browser) it can take a while until the change is propagated to all contexts. To synchronize, atomic operations are needed.

APIs which use SharedArrayBuffer objects

Security requirements

Shared memory and high-resolution timers were effectively disabled at the start of 2018 in light of Spectre. In 2020, a new, secure approach has been standardized to re-enable shared memory. With a few security measures, postMessage() will no longer throw for SharedArrayBuffer objects and shared memory across threads will be available:

As a baseline requirement, your document needs to be in a secure context.

For top-level documents, two headers will need to be set to cross-origin isolate your site:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

To check if cross origin isolation has been successful, you can test against the crossOriginIsolated property available to window and worker contexts:

if (crossOriginIsolated) {
  // Post SharedArrayBuffer
} else {
  // Do something else
}

See also Planned changes to shared memory which is starting to roll out to browsers (Firefox 79, for example.)

Always use the new operator to create a SharedArrayBuffer

SharedArrayBuffer constructors are required to be constructed with a new operator. Calling a SharedArrayBuffer constructor as a function without new will throw a TypeError.

var sab = SharedArrayBuffer(1024);
// TypeError: calling a builtin SharedArrayBuffer constructor 
// without new is forbidden
var sab = new SharedArrayBuffer(1024);

Constructor

SharedArrayBuffer()
Creates a new SharedArrayBuffer object.

Instance properties

SharedArrayBuffer.prototype.byteLength
The size, in bytes, of the array. This is established when the array is constructed and cannot be changed. Read only.

Instance methods

SharedArrayBuffer.prototype.slice(begin, end)
Returns a new SharedArrayBuffer whose contents are a copy of this SharedArrayBuffer's bytes from begin, inclusive, up to end, exclusive. If either begin or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.

Examples

Creating a new SharedArrayBuffer

var sab = new SharedArrayBuffer(1024);

Slicing the SharedArrayBuffer

sab.slice();    // SharedArrayBuffer { byteLength: 1024 }
sab.slice(2);   // SharedArrayBuffer { byteLength: 1022 }
sab.slice(-2);  // SharedArrayBuffer { byteLength: 2 }
sab.slice(0, 1); // SharedArrayBuffer { byteLength: 1 }

Using it in a WebGL buffer

const canvas = document.querySelector('canvas');
const gl = canvas.getContext('webgl');
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, sab, gl.STATIC_DRAW);

Specifications

Specification
ECMAScript (ECMA-262)The definition of 'SharedArrayBuffer' in that specification.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
SharedArrayBuffer

Chrome Full support 68


Full support 68


No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This was a temporary removal while mitigations were put in place.

Edge Full support 79


Full support 79


No support 16 — 17

Notes'

Notes' Support was removed to mitigate speculative execution side-channel attacks (Windows blog).

Firefox Full support 79


Full support 79


Full support 57

Notes' Disabled'

Notes' Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog). Disabled' From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 55 — 57


No support 46 — 55

Disabled'

Disabled' From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

IE

No support No

Opera

No support No

Safari

No support 10.1 — 11

WebView Android No support 60 — 63

Notes'

No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

Chrome Android No support 60 — 63

Notes'

No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

Firefox Android Full support 79


Full support 79


Full support 57

Notes' Disabled'

Notes' Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog). Disabled' From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 55 — 57


No support 46 — 55

Disabled'

Disabled' From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

Opera Android

No support No

Safari iOS

No support 10.3 — 11

Samsung Internet Android No support No

Notes'

No support No

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

nodejs

Full support 8.10.0

SharedArrayBuffer() constructor

Chrome Full support 68


Full support 68


No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This was a temporary removal while mitigations were put in place.

Edge Full support 79


Full support 79


No support 16 — 17

Notes'

Notes' Support was removed to mitigate speculative execution side-channel attacks (Windows blog).

Firefox Full support 79


Full support 79


Full support 57

Notes' Disabled'

Notes' Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog). Disabled' From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 55 — 57


No support 46 — 55

Disabled'

Disabled' From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

IE

No support No

Opera

No support No

Safari

No support 10.1 — 11

WebView Android No support 60 — 63

Notes'

No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

Chrome Android No support 60 — 63

Notes'

No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

Firefox Android Full support 79


Full support 79


Full support 57

Notes' Disabled'

Notes' Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog). Disabled' From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 55 — 57


No support 46 — 55

Disabled'

Disabled' From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

Opera Android

No support No

Safari iOS

No support 10.3 — 11

Samsung Internet Android No support No

Notes'

No support No

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

nodejs

Full support 8.10.0

byteLength

Chrome Full support 68


Full support 68


No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This was a temporary removal while mitigations were put in place.

Edge Full support 79


Full support 79


No support 16 — 17

Notes'

Notes' Support was removed to mitigate speculative execution side-channel attacks (Windows blog).

Firefox Full support 79


Full support 79


Full support 57

Notes' Disabled'

Notes' Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog). Disabled' From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 55 — 57


No support 46 — 55

Disabled'

Disabled' From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

IE

No support No

Opera

No support No

Safari

No support 10.1 — 11

WebView Android No support 60 — 63

Notes'

No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

Chrome Android No support 60 — 63

Notes'

No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

Firefox Android Full support 79


Full support 79


Full support 57

Notes' Disabled'

Notes' Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog). Disabled' From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 55 — 57


No support 46 — 55

Disabled'

Disabled' From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

Opera Android

No support No

Safari iOS

No support 10.3 — 11

Samsung Internet Android No support No

Notes'

No support No

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

nodejs

Full support 8.10.0

slice

Chrome Full support 68


Full support 68


No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This was a temporary removal while mitigations were put in place.

Edge Full support 79


Full support 79


No support 16 — 17

Notes'

Notes' Support was removed to mitigate speculative execution side-channel attacks (Windows blog).

Firefox Full support 79


Full support 79


Full support 57

Notes' Disabled'

Notes' Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog). Disabled' From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 55 — 57


No support 46 — 55

Disabled'

Disabled' From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

IE

No support No

Opera

No support No

Safari

No support 10.1 — 11

WebView Android No support 60 — 63

Notes'

No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

Chrome Android No support 60 — 63

Notes'

No support 60 — 63

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

Firefox Android Full support 79


Full support 79


Full support 57

Notes' Disabled'

Notes' Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog). Disabled' From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config. No support 55 — 57


No support 46 — 55

Disabled'

Disabled' From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

Opera Android

No support No

Safari iOS

No support 10.3 — 11

Samsung Internet Android No support No

Notes'

No support No

Notes'

Notes' Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.

nodejs

Full support 8.10.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.'
See implementation notes.
User must explicitly enable this feature.'
User must explicitly enable this feature.


See also