docs.rodeo

MDN Web Docs mirror

SharedArrayBuffer.prototype.slice()

{{JSRef}} 

The slice() method of {{jsxref("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.

{{InteractiveExample("JavaScript Demo: SharedArrayBuffer.slice()")}} 

// Create a SharedArrayBuffer with a size in bytes
const buffer = new SharedArrayBuffer(16);
const int32View = new Int32Array(buffer); // Create the view
// Produces Int32Array [0, 0, 0, 0]

int32View[1] = 42;
const sliced = new Int32Array(buffer.slice(4, 12));

console.log(sliced);
// Expected output: Int32Array [42, 0]

Syntax

slice()
slice(start)
slice(start, end)

Parameters

Return value

A new {{jsxref("SharedArrayBuffer")}}  containing the extracted elements.

Examples

Using slice()

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 }

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN