JavaScript SharedArrayBuffer slice() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

buffer.slice(begin, end) returns a new SharedArrayBuffer that holds a copy of the bytes from begin up to, but not including, end. The original buffer is not changed. Negative positions count back from the end, just like Array slice().

The important word is copy. The new buffer is separate memory, so it does not share data with the original, even though both are SharedArrayBuffers. Use slice() when you want a snapshot of the data at one moment. Use a typed array view when you want to keep looking at the same shared bytes.

Remember
buffer.slice(begin, end)   → new SharedArrayBuffer (a copy)

slice(0, 4)   → bytes 0, 1, 2, 3
slice(-2)     → the last 2 bytes
slice()       → copy of the whole buffer
slice(4, 1)   → empty buffer (byteLength 0)

copy, not a view: later changes do not flow in either direction

Syntax

Call slice() on a SharedArrayBuffer with zero, one, or two positions.

JavaScript
const buffer = new SharedArrayBuffer(6);
new Uint8Array(buffer).set([10, 20, 30, 40, 50, 60]);

const all = buffer.slice();
const tail = buffer.slice(4);
const middle = buffer.slice(1, 4);

console.log(all.byteLength);      // 6
console.log(tail.byteLength);     // 2
console.log(middle.byteLength);   // 3
CallWhat it does
buffer.slice()Copy the whole buffer.
buffer.slice(begin)Copy from begin to the end.
buffer.slice(begin, end)Copy from begin up to, not including, end.
new Uint8Array(buffer, offset, length)A view instead of a copy: it shows the same shared bytes and copies nothing.

A standard JavaScript method (ES2017) on every SharedArrayBuffer, from SharedArrayBuffer.prototype. It works on growable and fixed-length buffers. Siblings: grow().

Library declarations
JavaScript
// ECMAScript 2017
buffer.slice(begin, end)   // returns a new SharedArrayBuffer

// begin: default 0; negative counts from the end
// end:   default buffer.byteLength; negative counts from the end
// both are clamped to the range 0 .. byteLength

Parameters

ParameterDescription
beginOptional. The index of the first byte to copy (default 0). A negative value counts from the end: -2 means two bytes before the end.
endOptional. The index to stop before (default byteLength). Negative values count from the end, and values past the end are clamped.

Return Value

Return ValueDescription
SharedArrayBufferA brand-new, fixed-length SharedArrayBuffer with the copied bytes. Its byteLength is end - begin, or 0 when end is not after begin.
TypeError (thrown)Only when slice() is borrowed and called on something that is not a SharedArrayBuffer, such as a normal ArrayBuffer.

JavaScript Examples

Example 1 — Copy a Range of Bytes

Fill a 6-byte shared buffer, copy the bytes from index 1 up to index 4, and look at the copy.

JavaScript
const buffer = new SharedArrayBuffer(6);
const bytes = new Uint8Array(buffer);
bytes.set([10, 20, 30, 40, 50, 60]);

const part = buffer.slice(1, 4);

console.log("byteLength: " + part.byteLength);
console.log("Bytes: " + new Uint8Array(part).join(","));
console.log("Is a SharedArrayBuffer: " + (part instanceof SharedArrayBuffer));
console.log("Same object as the original: " + (part === buffer));
console.log("Original byteLength: " + buffer.byteLength);
Try It Yourself

The Try It page first checks that SharedArrayBuffer is available. A normal browser tab is not cross-origin isolated, so there it prints a short explanation and the output above. In Node.js, or on a cross-origin isolated page, it runs the code for real.

How It Works

  1. Fill the buffer. The view bytes writes six numbers into the shared buffer, at indexes 0 to 5.
  2. Copy a range. slice(1, 4) copies indexes 1, 2 and 3. Index 4 is where it stops and is not included, so the copy holds 20,30,40.
  3. A new buffer. The copy is a different SharedArrayBuffer object, and the original still has all 6 bytes.

Example 2 — Take a Snapshot of Shared Data

Copy the whole buffer to keep the values from one moment, then change the live data and compare.

JavaScript
const buffer = new SharedArrayBuffer(8);
const live = new Int32Array(buffer);
live[0] = 5;
live[1] = 10;

const snapshot = new Int32Array(buffer.slice());

live[0] = 500;
live[1] = 1000;

console.log("Live values: " + live.join(","));
console.log("Snapshot values: " + snapshot.join(","));
console.log("Snapshot shares memory with live: " + (snapshot.buffer === buffer));
Try It Yourself

The Try It page first checks that SharedArrayBuffer is available. A normal browser tab is not cross-origin isolated, so there it prints a short explanation and the output above. In Node.js, or on a cross-origin isolated page, it runs the code for real.

How It Works

  1. Copy everything. buffer.slice() with no positions copies all 8 bytes at that moment, and new Int32Array(...) lets you read the copy as two numbers.
  2. Change the live data. Writing 500 and 1000 through live changes the original shared buffer only.
  3. The snapshot stays the same. It still shows 5,10, because the copy has its own memory. Its buffer is not the same object as the original.

Example 3 — Use Negative Positions and Odd Ranges

slice() never throws because of its positions. It counts negative values from the end and clamps the rest. This example also slices a growable buffer.

JavaScript
const buffer = new SharedArrayBuffer(6, { maxByteLength: 12 });
new Uint8Array(buffer).set([10, 20, 30, 40, 50, 60]);

const show = (buf) => new Uint8Array(buf).join(",");

console.log("slice(-2): " + show(buffer.slice(-2)));
console.log("slice(1, -3): " + show(buffer.slice(1, -3)));
console.log("slice(2, 100): " + show(buffer.slice(2, 100)));
console.log("slice(4, 1) length: " + buffer.slice(4, 1).byteLength);
console.log("Source growable: " + buffer.growable);
console.log("Copy growable: " + buffer.slice().growable);
Try It Yourself

The Try It page first checks that SharedArrayBuffer and growable buffers (maxByteLength) are available. A normal browser tab is not cross-origin isolated, so there it prints a short explanation and the output above. In Node.js, or on a cross-origin isolated page, it runs the code for real.

How It Works

  1. Negative begin. slice(-2) starts two bytes before the end, at index 4, and copies the last two bytes.
  2. Mixed and clamped ends. An end of -3 means index 3, so slice(1, -3) copies indexes 1 and 2. An end of 100 is clamped to 6. When the end is not after the begin, you get an empty buffer and no error.
  3. The copy is fixed-length. The source was made with maxByteLength, so it is growable. The copy is not growable, so grow() would throw a TypeError on it.

Browser & Runtime Support

SharedArrayBuffer.prototype.slice() is part of ES2017, together with SharedArrayBuffer. Browsers only provide SharedArrayBuffer on cross-origin isolated pages.

✓ ES2017 · Widely supported

sharedBuffer.slice()

Supported in Chrome 68+, Edge 79+, Firefox 79+, Safari 15.2+, Opera 55+ and Node.js 8.10+. Internet Explorer never got it.

ES2017 Standard since
Google Chrome 68+ · Desktop & Mobile
Full support
Microsoft Edge 79+ · Chromium based
Full support
Mozilla Firefox 79+ · Desktop & Mobile
Full support
Apple Safari 15.2+ · macOS & iOS
Full support
Opera 55+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 68+ · Chromium based
Supported
Node.js 8.10+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
sharedBuffer.slice() Excellent

Bottom line: Safe in every current browser and in Node.js, as long as SharedArrayBuffer itself is available. In browsers the page must be cross-origin isolated, so check typeof SharedArrayBuffer !== "undefined" first.

Frequently Asked Questions

A new SharedArrayBuffer that contains a copy of the selected bytes. The original buffer keeps all of its bytes and its size.
No. slice() copies the bytes into new memory, so changes to the original do not appear in the copy, and the other way round. To keep working on the same shared bytes, use a typed array view on the original buffer, and send that buffer to the other thread.
slice() copies bytes into a new buffer. A view such as new Uint8Array(buffer, 2, 3) is a window onto the same buffer, so nothing is copied and writes are shared. Use slice() for a snapshot and a view for live data.
Yes. Negative values count from the end, so buffer.slice(-4) copies the last four bytes. Positions that are too large are clamped, and an end that is not after the begin gives an empty buffer. Positions never throw an error.
No. Even if you slice a growable buffer, the copy is a fixed-length SharedArrayBuffer and its growable property is false. If you need a growable buffer, create a new one with maxByteLength and copy the bytes into it.

Back to the method index

See both SharedArrayBuffer method guides grouped by what they do, with a short description for each.

SharedArrayBuffer Methods index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful