slice
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 (!)
Content copied to clipboard
Since
v0.3.0
Parameters
end=buf.length Where the new Buffer
will end (not inclusive).