Buffer.slice - Node documentation
method Buffer.slice

Usage in Deno

import { type Buffer } from "node:buffer";
Buffer.slice(
start?: number,
end?: number,
): Buffer
Deprecated

Use subarray instead.

Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.

This method is not compatible with the Uint8Array.prototype.slice(), which is a superclass of Buffer. To copy the slice, useUint8Array.prototype.slice().

import { Buffer } from 'node:buffer';

const buf = Buffer.from('buffer');

const copiedBuf = Uint8Array.prototype.slice.call(buf);
copiedBuf[0]++;
console.log(copiedBuf.toString());
// Prints: cuffer

console.log(buf.toString());
// Prints: buffer

// With buf.slice(), the original buffer is modified.
const notReallyCopiedBuf = buf.slice();
notReallyCopiedBuf[0]++;
console.log(notReallyCopiedBuf.toString());
// Prints: cuffer
console.log(buf.toString());
// Also prints: cuffer (!)

Parameters

optional
start: number = 0

Where the new Buffer will start.

optional
end: number = buf.length

Where the new Buffer will end (not inclusive).

Return Type