`SharedArrayBuffer` 对象用来表示一个通用的，固定长度的原始二进制数据缓冲区，类似于 `ArrayBuffer` 对象，它们都可以用来在共享内存（shared memory）上创建视图。与 `ArrayBuffer` 不同的是，`SharedArrayBuffer` 不能被分离。

请注意，由于能够轻易地利用《边信道（side-channel）读取未授权内存的攻击技术》中提到的Spectre漏洞——一种利用现代 CPU 使用的执行优化功能的新攻击技术， `SharedArrayBuffer` 功能将在 Chrome 和 FireFox 的新版本中禁用，并将逐渐被所有浏览器禁用。

但该API并不会被废弃，chrome团队声明在未来解决因web时钟导致的安全问题后，该API将会恢复。

## 语法

    new SharedArrayBuffer(length)

### 参数

`length`
  所创建的数组缓冲区的大小，以字节(byte)为单位。

### 返回值

一个大小指定的新 `SharedArrayBuffer` 对象。其内容被初始化为 0。

## 描述

### 分配及共享内存

为了将一个`SharedArrayBuffer` 对象从一个用户代理共享到另一个用户代理（另一个页面的主进程或者当前页面的一个 `worker` ）从而实现共享内存，我们需要运用 `postMessage` 和结构化克隆算法（ structured cloning ）。

结构化克隆算法接收被映射到一个新的 `SharedArrayBuffers` 对象上的 `SharedArrayBuffers` 对象与 `TypedArrays` 对象。在这两种映射下，这个新的 `SharedArrayBuffer` 对象会被传递到目标用户代理的接收函数上，导致在目标用户代理产生了一个新的私有 `SharedArrayBuffer` 对象（正如 `ArrayBuffer` 一样）。然而，这两个 `SharedArrayBuffer` 对象指向的共享数据块其实是同一个，并且在某一代理中的一个块的副作用将最终导致另一个代理具有可见性。

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

在早期版本的特性中，结构化克隆期间 `SharedArrayBuffers` 需要被精确转化。然而，从HTML的意义上来讲，`SharedArrayBuffer`s 并非一个 `Transferable object`。因此，如果一个 `SharedArrayBuffer` 对象位于转化队列，`postMessage` 将会抛出一个 `DataCloneError` 或 一个 `DOMException` ：

    var sab = new SharedArrayBuffer(1024);
    worker.postMessage(sab, [sab]);
    // Uncaught DOMException: Failed to execute 'postMessage' on 'Worker': A SharedArrayBuffer could not be cloned.

### 通过原子操作更新及同步来共享内存

共享内存能被同时创建和更新于工作者线程或主线程。依赖于系统（CPU，操作系统，浏览器），变化传递给环境需要一段时间。需要通过 atomic 操作来进行同步。

### 接受 `SharedArrayBuffer` 对象的API

- `WebGLRenderingContext.bufferData()`
- `WebGLRenderingContext.bufferSubData()`
- `WebGL2RenderingContext.getBufferSubData()`

### 需要 `new` 运算符来构造

`SharedArrayBuffer` 需要 `new` 运算符来构造一个构造函数. 作为函数来调用一个 `SharedArrayBuffer` 构造函数时，如果不用 `new` 运算符，将会抛出一个 `TypeError` 异常。

    var sab = SharedArrayBuffer(1024);
    // TypeError: calling a builtin SharedArrayBuffer constructor
    // 必须用 new 来构造

    var sab = new SharedArrayBuffer(1024);

## 属性

`SharedArrayBuffer.length`
  `SharedArrayBuffer` 构造函数的 length 属性值为1。
`SharedArrayBuffer.prototype`
  允许所有 `SharedArrayBuffer` 对象的附加属性。

## `SharedArrayBuffer` 原型对象

所有 `SharedArrayBuffer` 实例继承自 `SharedArrayBuffer.prototype`。

### 属性

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` 只读
  The size, in bytes, of the array. This is established when the array is constructed and cannot be changed. Read only.

### 方法

`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.

## 规范

规范 | 状态 | 备注
--- | --- | ---
ECMAScript Latest Draft (ECMA-262) SharedArrayBuffer | Draft | Initial definition in ES2017.
ECMAScript 2017 (ECMA-262) SharedArrayBuffer | Standard |

## 浏览器兼容性

Update compatibility data on GitHub

| Desktop | Mobile | Server
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | 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 No support 16 — 17 Notes No support 16 — 17 Notes Notes Support was removed to mitigate speculative execution side-channel attacks (Windows blog). | Firefox Full support 57 Notes Disabled 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 — ? | 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. | Edge Mobile ? | Firefox Android Full support 57 Notes Disabled 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 No | Samsung Internet Android No support No | nodejs Full support 8.10.0
`SharedArrayBuffer` in `DataView` | 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 No support No | Firefox Full support 57 Notes Disabled 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 — ? | 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. | Edge Mobile ? | Firefox Android Full support 57 Notes Disabled 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 No | Samsung Internet Android No support No | nodejs No support No
`prototype` | 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 No support 16 — 17 Notes No support 16 — 17 Notes Notes Support was removed to mitigate speculative execution side-channel attacks (Windows blog). | Firefox Full support 57 Notes Disabled 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 — ? | 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. | Edge Mobile ? | Firefox Android Full support 57 Notes Disabled 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 No | Samsung Internet Android No support No | nodejs No support No
`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 No support 16 — 17 Notes No support 16 — 17 Notes Notes Support was removed to mitigate speculative execution side-channel attacks (Windows blog). | Firefox Full support 57 Notes Disabled 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 — ? | 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. | Edge Mobile ? | Firefox Android Full support 57 Notes Disabled 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 No | Samsung Internet Android No support No | 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 No support 16 — 17 Notes No support 16 — 17 Notes Notes Support was removed to mitigate speculative execution side-channel attacks (Windows blog). | Firefox Full support 57 Notes Disabled 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 — ? | 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. | Edge Mobile ? | Firefox Android Full support 57 Notes Disabled 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 No | Samsung Internet Android No support No | nodejs Full support 8.10.0

### Legend

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

## 相关链接

- `Atomics`
- `ArrayBuffer`
- JavaScript typed arrays
- Web Workers
- parlib-simple – a simple library providing synchronization and work distribution abstractions.
- Shared Memory – a brief tutorial

- A Taste of JavaScript’s New Parallel Primitives – Mozilla Hacks