SharedArrayBuffer
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.
Note that SharedArrayBuffer was disabled by default in all major browsers on 5 January, 2018 in response to Spectre. Chrome re-enabled it in v67 on platforms where its site-isolation feature is enabled to protect against Spectre-style vulnerabilities.
Syntax
new SharedArrayBuffer(length)
Parameters
length - The size, in bytes, of the array buffer to create.
Return value
A new SharedArrayBuffer object of the specified size. Its contents are initialized to 0.
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 accepting SharedArrayBuffer objects
Constructing is required with new operator
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);
Properties
SharedArrayBuffer.length - The
SharedArrayBuffer constructor's length property whose value is 1. SharedArrayBuffer.prototype - Allows the addition of properties to all
SharedArrayBuffer objects.
SharedArrayBuffer prototype object
All SharedArrayBuffer instances inherit from SharedArrayBuffer.prototype.
Properties
- SharedArrayBuffer.prototype.constructor
- Specifies the function that creates an object's prototype. The initial value is the standard built-in
SharedArrayBuffer constructor. -
SharedArrayBuffer.prototype.byteLength Read only
- The size, in bytes, of the array. This is established when the array is constructed and cannot be changed. Read only.
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.
Specifications
|
Desktop |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
| Basic support |
68
- 68
- 60 — 63
- 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.
|
No
|
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
10.1 — ? |
SharedArrayBuffer in DataView
|
68
- 68
- 60 — 63
- 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.
|
No
|
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
10.1 — ? |
prototype |
68
- 68
- 60 — 63
- 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.
|
No
|
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
10.1 — ? |
byteLength |
68
- 68
- 60 — 63
- 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.
|
16
|
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
10.1 — ? |
slice |
68
- 68
- 60 — 63
- 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.
|
16
|
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
10.1 — ? |
|
Mobile |
|
Android webview |
Chrome for Android |
Edge Mobile |
Firefox for Android |
Opera for Android |
iOS Safari |
Samsung Internet |
| Basic support |
60 — 63
- 60 — 63
- 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.
|
60 — 63
- 60 — 63
- 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.
|
? |
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
No |
SharedArrayBuffer in DataView
|
60 — 63
- 60 — 63
- 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.
|
60 — 63
- 60 — 63
- 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.
|
? |
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
No |
prototype |
60 — 63
- 60 — 63
- 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.
|
60 — 63
- 60 — 63
- 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.
|
? |
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
No |
byteLength |
60 — 63
- 60 — 63
- 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.
|
60 — 63
- 60 — 63
- 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.
|
? |
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
No |
slice |
60 — 63
- 60 — 63
- 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.
|
60 — 63
- 60 — 63
- 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.
|
? |
57 Disabled
- 57
Disabled
- 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.
- 55 — 57
- 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.
|
No |
No |
No |
See also