Convert a typed array to a Buffer without a copy
npx @tessl/cli install tessl/npm-typedarray-to-buffer@4.0.0Convert a typed array to a Buffer without a copy. This lightweight utility provides efficient conversion between typed arrays and Node.js Buffer objects by leveraging the underlying ArrayBuffer to avoid expensive memory copies.
npm install typedarray-to-bufferconst toBuffer = require('typedarray-to-buffer');For environments supporting ES modules:
import toBuffer from 'typedarray-to-buffer';const toBuffer = require('typedarray-to-buffer');
// Convert a Uint8Array to Buffer without copying
const uint8Array = new Uint8Array([1, 2, 3]);
const buffer = toBuffer(uint8Array);
console.log(buffer); // <Buffer 01 02 03>
console.log(Buffer.isBuffer(buffer)); // true
console.log(buffer instanceof Uint8Array); // true (original typed array augmented)
// Works with all typed array types
const uint32Array = new Uint32Array([1, 2, 3]);
const buffer32 = toBuffer(uint32Array);
console.log(buffer32); // <Buffer 01 00 00 00 02 00 00 00 03 00 00 00> (little-endian)
// Handles array views correctly
const subArray = new Uint8Array([1, 2, 3, 4, 5]).subarray(1, 4);
const subBuffer = toBuffer(subArray);
console.log(subBuffer); // <Buffer 02 03 04>Converts typed arrays to Buffer objects without copying data when possible, falling back to standard Buffer creation for non-typed array inputs.
/**
* Convert a typed array to a Buffer without a copy
* @param {any} arr - Input value (typed array or any value supported by Buffer.from)
* @returns {Buffer} A Buffer object created from the input
*/
function typedarrayToBuffer(arr)Parameters:
arr (any): The input value to convert. Can be:
Returns:
Buffer: A Buffer object created from the inputBehavior:
Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength). This respects the typed array's view (byteOffset and byteLength) and avoids data copying.Buffer.from(arr) with standard Buffer creation behavior.ArrayBuffer.isView(arr) to determine if the input is a typed array.Supported Typed Array Types:
Uint8Array, Uint8ClampedArrayUint16Array, Uint32ArrayInt8Array, Int16Array, Int32ArrayFloat32Array, Float64ArrayUsage Examples:
const toBuffer = require('typedarray-to-buffer');
// Basic conversion
const arr = new Uint8Array([1, 2, 3]);
const buf = toBuffer(arr);
console.log(buf.toString('hex')); // '010203'
// Preserves byte ordering for multi-byte types
const arr32 = new Uint32Array([0x12345678]);
const buf32 = toBuffer(arr32);
console.log(buf32.toString('hex')); // '78563412' (little-endian on most systems)
// Handles array views (subarray)
const original = new Uint8Array([10, 20, 30, 40, 50]);
const view = original.subarray(1, 4); // [20, 30, 40]
const viewBuf = toBuffer(view);
console.log(viewBuf.toString()); // Buffer contains [20, 30, 40]
// Non-typed array fallback
const regularArray = [1, 2, 3];
const fallbackBuf = toBuffer(regularArray);
console.log(fallbackBuf); // Standard Buffer.from() behaviorThe function relies on Node.js's Buffer.from() for error handling:
Buffer.from() which may throw for unsupported typesBuffer.from() implementationBuffer.from() for environments without typed array supportbyteOffset and byteLength of typed array views, ensuring correct data boundaries