or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-typedarray-to-buffer

Convert a typed array to a Buffer without a copy

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/typedarray-to-buffer@4.0.x

To install, run

npx @tessl/cli install tessl/npm-typedarray-to-buffer@4.0.0

index.mddocs/

typedarray-to-buffer

Convert 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.

Package Information

  • Package Name: typedarray-to-buffer
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install typedarray-to-buffer

Core Imports

const toBuffer = require('typedarray-to-buffer');

For environments supporting ES modules:

import toBuffer from 'typedarray-to-buffer';

Basic Usage

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>

Capabilities

TypedArray to Buffer Conversion

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:
    • Any typed array (Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float32Array, Float64Array)
    • Any ArrayBufferView
    • Any other value supported by Buffer.from() (falls back to standard Buffer creation)

Returns:

  • Buffer: A Buffer object created from the input

Behavior:

  • For typed arrays: Creates a Buffer using the underlying ArrayBuffer with Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength). This respects the typed array's view (byteOffset and byteLength) and avoids data copying.
  • For non-typed arrays: Falls back to Buffer.from(arr) with standard Buffer creation behavior.
  • Type detection: Uses ArrayBuffer.isView(arr) to determine if the input is a typed array.

Supported Typed Array Types:

  • Uint8Array, Uint8ClampedArray
  • Uint16Array, Uint32Array
  • Int8Array, Int16Array, Int32Array
  • Float32Array, Float64Array
  • Any other ArrayBufferView implementation

Usage 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() behavior

Error Handling

The function relies on Node.js's Buffer.from() for error handling:

  • Invalid typed array inputs are handled by the underlying ArrayBuffer operations
  • Non-typed array inputs are processed by Buffer.from() which may throw for unsupported types
  • No explicit error handling is performed by the library itself

Platform Compatibility

  • Node.js: Uses native Buffer.from() implementation
  • Browser: Compatible with environments supporting typed arrays and Buffer polyfill (e.g., via browserify or webpack)
  • Legacy browsers: Graceful fallback through Buffer.from() for environments without typed array support

Performance Notes

  • Zero-copy optimization: When converting typed arrays, the operation creates a Buffer view of the same underlying memory, avoiding expensive data copying
  • Memory efficiency: The returned Buffer shares the same ArrayBuffer as the original typed array
  • View preservation: Respects byteOffset and byteLength of typed array views, ensuring correct data boundaries