JavaScript SharedArrayBuffer.prototype.slice() Method
Last Updated :
09 Jan, 2024
JavaScript SharedArrayBuffer.prototype.slice() method is specifically for the objects of the SharedArrayBuffer type. It is used to create a new SharedArrayBuffer object that references the same memory region as the original but with a different range.
Syntax:
sharedArrayBufferVariable.slice(start, end);
Parameters:
- start: An optional parameter representing the start index.
- end: An optional parameter representing the end index. If omitted, the new SharedArrayBuffer will extend to the end of the original buffer.
Return Value:
- It returns a new SharedArrayBuffer object representing the specified portion of the original buffer.
Example 1: The code example practically implements the SharedArrayBuffer.prototype.slice() method.
Javascript
const buffer = new SharedArrayBuffer(16);
const intArray = new Int32Array(buffer);
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
intArray[3] = 4;
const GFG = intArray.slice(0, 2);
console.log( "Original Array:" , intArray);
console.log( "New Array:" , GFG);
|
Output
Original Array: Int32Array(4) [ 1, 2, 3, 4 ]
New Array: Int32Array(2) [ 1, 2 ]
Example 2: The below code example also implements the slice() method of SharedArrayBuffer.
Javascript
const Buffer = new SharedArrayBuffer(8);
const Array = new Uint8Array(Buffer);
const slicedArray = Array.slice(1, 5);
const slicedBuffer = slicedArray.buffer;
console.log(slicedBuffer.byteLength);
|
Share your thoughts in the comments
Please Login to comment...