From: Sam Sudar Date: Sat, 1 Jul 2017 05:14:16 +0000 (-0700) Subject: Add slice for converting to ArrayBuffer X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=65daaff0369d4fbbedcb181ea04089a8df827ee0;p=buffer.git Add slice for converting to ArrayBuffer The .buffer property is an ArrayBuffer, but it is not necessarily the same length as the original buffer. This adds further documentation to the README to flesh out converting a Buffer to an ArrayBuffer. --- diff --git a/README.md b/README.md index 6acfd22..e925100 100644 --- a/README.md +++ b/README.md @@ -138,9 +138,13 @@ var buffer = Buffer.from(arrayBuffer) To convert a `Buffer` to an `ArrayBuffer`, use the `.buffer` property (which is present on all `Uint8Array` objects): ```js -var arrayBuffer = buffer.buffer +var arrayBuffer = buffer.buffer.slice( + buffer.byteOffset, buffer.byteOffset + buffer.byteLength +) ``` +Alternatively, use the [`to-arraybuffer`](https://www.npmjs.com/package/to-arraybuffer) module. + ## performance See perf tests in `/perf`.