The slice() method of SharedArrayBuffer instances returns a new SharedArrayBuffer whose contents are a copy of this SharedArrayBuffer's bytes from start, inclusive, up to end, exclusive. If either start or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.
slice()
slice(start)
slice(start, end)start OptionalZero-based index at which to start extraction, converted to an integer.
-buffer.length <= start < 0, start + buffer.length is used.start < -buffer.length or start is omitted, 0 is used.start >= buffer.length, an empty buffer is returned.end OptionalZero-based index at which to end extraction, converted to an integer. slice() extracts up to but not including end.
-buffer.length <= end < 0, end + buffer.length is used.end < -buffer.length, 0 is used.end >= buffer.length or end is omitted or undefined, buffer.length is used, causing all elements until the end to be extracted.end implies a position before or at the position that start implies, an empty buffer is returned.A new SharedArrayBuffer containing the extracted elements.
Note that these examples cannot be run directly from the console or an arbitrary web page, because SharedArrayBuffer is not defined unless its security requirements are met.
// Create a SharedArrayBuffer with a size in bytes
const buffer = new SharedArrayBuffer(16);
const int32View = new Int32Array(buffer);
int32View[1] = 42;
// Produces Int32Array [0, 42, 0, 0]
const sliced = new Int32Array(buffer.slice(4, 12));
console.log(sliced); // Int32Array [42, 0]const sab = new SharedArrayBuffer(1024);
sab.slice(); // SharedArrayBuffer { byteLength: 1024 }
sab.slice(2); // SharedArrayBuffer { byteLength: 1022 }
sab.slice(-2); // SharedArrayBuffer { byteLength: 2 }
sab.slice(0, 1); // SharedArrayBuffer { byteLength: 1 }