This page is the JavaScript SharedArrayBuffer Methods index. A SharedArrayBuffer has two methods of its own. Each row below links to a full guide with syntax, examples you can run, and FAQs.
Learn what a SharedArrayBuffer is on /js/shared-array-buffer. Use this page when you need a specific method such as grow().
Both methods are called on a buffer.
First create one with new SharedArrayBuffer(length). Then call buffer.grow() to make a growable buffer bigger, or buffer.slice() to copy a range of bytes. They are instance methods, not static ones.
Syntax
How to Call a SharedArrayBuffer Method
Create a buffer, then call the methods on it:
JavaScript
const buffer = new SharedArrayBuffer(4, { maxByteLength: 16 });
buffer.grow(8); // make it bigger
console.log(buffer.byteLength); // 8
const copy = buffer.slice(0, 4); // copy the first 4 bytes
console.log(copy.byteLength); // 4
console.log(copy === buffer); // false
Browse
JavaScript SharedArrayBuffer Methods Table
All method guides in this JavaScript tutorial series (2 tutorials), grouped by what they do.
Change the size
1 method
Make a shared buffer bigger without creating a new one.
These pieces work together with the two methods. They are not methods you call on the buffer.
Syntax
What it does
new SharedArrayBuffer(length, { maxByteLength })
Creates a shared buffer. The maxByteLength option makes it growable.
buffer.byteLength
The current size of the buffer in bytes. It gets bigger after grow().
buffer.growable
Is true when the buffer was made with maxByteLength, otherwise false.
buffer.maxByteLength
The size limit that grow() may not go over.
new Int32Array(buffer)
A typed array view that reads and writes the shared bytes. Without a length it follows a growing buffer.
Atomics.add(view, index, value)
Changes a shared number in one safe step when several threads work on the buffer.
Frequently Asked Questions
Two: grow() makes a growable buffer bigger in place, and slice() copies a range of bytes into a new SharedArrayBuffer. It also has the properties byteLength, growable and maxByteLength.
The SharedArrayBuffer page explains the idea: shared memory, views, Atomics and how it compares with ArrayBuffer. This index lists the method guides so you can jump straight to grow() or slice().
No. Both are instance methods. You call them on a buffer that you created with new, as in buffer.grow(16), and not on SharedArrayBuffer itself.
No. A SharedArrayBuffer can only grow, with grow(). The methods resize() and transfer() belong to ArrayBuffer, because a shared buffer cannot shrink and is never moved away from its owner.
Start with grow() to learn how growable buffers and maxByteLength work, then read slice() to learn how to copy bytes. If you want the big picture first, read the SharedArrayBuffer page.