This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
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.
new SharedArrayBuffer(length)
length
A new SharedArrayBuffer
object of the specified size. Its contents are initialized to 0.
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);
In earlier versions of the specification, SharedArrayBuffer
s needed to be explicitly transferred during structured cloning. However, a SharedArrayBuffer
is not a Transferable
object in the sense of HTML. Thus if a SharedArrayBuffer
object is present in the transfer list, then postMessage
will throw a DataCloneError
or at least a warning:
var sab = new SharedArrayBuffer(1024); worker.postMessage(sab, [sab]); // SharedArrayBuffer must not be in the transfer list
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.
SharedArrayBuffer
objectsWebGLRenderingContext.bufferData()
WebGLRenderingContext.bufferSubData()
WebGL2RenderingContext.getBufferSubData()
new
operatorSharedArrayBuffer
constructors require 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);
SharedArrayBuffer.length
SharedArrayBuffer
constructor's length property whose value is 1.SharedArrayBuffer.prototype
SharedArrayBuffer
objects.SharedArrayBuffer
prototype objectAll SharedArrayBuffer
instances inherit from SharedArrayBuffer.prototype
.
SharedArrayBuffer
constructor.SharedArrayBuffer.prototype.byteLength
Read only
SharedArrayBuffer.prototype.slice(begin, end)
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.Specification | Status | Comment |
---|---|---|
ECMAScript Shared Memory and Atomics The definition of 'SharedArrayBuffer' in that specification. | Draft | Initial definition. |
ECMAScript 2017 Draft (ECMA-262) | Draft |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | No support [2] | No support | 52 (52) [1] | No support | No support | No support |
slice() | No support | No support | 52 (52) [1] | No support | No support | No support |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | No support | 52.0 (52) [1] | No support | No support | No support |
slice() | No support | No support | 52.0 (52) [1] | No support | No support | No support |
[1] In Firefox version 46 until version 51, this feature was disabled by a preference setting. In about:config, set javascript.options.shared_memory
to true
.
[2] The implementation is under development and needs these runtime flags: --js-flags=--harmony-sharedarraybuffer --enable-blink-feature=SharedArrayBuffer
Atomics
ArrayBuffer
A Taste of JavaScript’s New Parallel Primitives – Mozilla Hacks
© 2005–2017 Mozilla Developer Network and individual 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