Uint8Array Utilities for JavaScript

5.1.1 · active · verified Sun Apr 19

The `uint8arrays` library provides a comprehensive set of utility functions to simplify operations with `Uint8Array`s, which are fundamental for handling binary data in modern JavaScript environments. Currently at version `5.1.1`, the library maintains an active release cadence, with frequent minor and patch updates addressing bug fixes and introducing new features. Its key differentiators include optimized performance in Node.js by transparently leveraging `Buffer` where beneficial, full TypeScript support with built-in type declarations, and extensive encoding capabilities for `fromString` and `toString` functions, including UTF-8 and a wide range of multibase encodings via its integration with the `multiformats` module. This package aims to bridge the gap of missing utility methods often found on Node.js `Buffer`s, making `Uint8Array` usage more convenient across browsers and Node.js alike.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates key utility functions: `alloc` (and `allocUnsafe` with caution), `fromString` with different encodings, `concat` for joining byte arrays, `toString` for decoding, and `equals` for comparison.

import { alloc, allocUnsafe } from 'uint8arrays/alloc';
import { concat } from 'uint8arrays/concat';
import { fromString } from 'uint8arrays/from-string';
import { toString } from 'uint8arrays/to-string';
import { equals } from 'uint8arrays/equals';

// Allocate a zero-filled Uint8Array
const bufferA: Uint8Array = alloc(10);
console.log('Allocated (zero-filled):', bufferA);

// Allocate an uninitialized Uint8Array (use with caution!)
const bufferUnsafe: Uint8Array = allocUnsafe(5);
console.log('Allocated (potentially uninitialized):', bufferUnsafe);
bufferUnsafe.fill(0xFF); // Always initialize unsafe buffers immediately
console.log('Unsafe buffer after fill:', bufferUnsafe);

// Convert a string to Uint8Array using UTF-8 encoding
const helloBytes: Uint8Array = fromString('Hello', 'utf8');
console.log('"Hello" as UTF-8 bytes:', helloBytes);

// Convert a string to Uint8Array using base64 encoding
const base64Input = 'SGVsbG8gV29ybGQ='; // 'Hello World' in base64
const helloWorldBytes: Uint8Array = fromString(base64Input, 'base64');
console.log(`"${base64Input}" as base64 bytes:`, helloWorldBytes);

// Concatenate multiple Uint8Arrays
const spaceBytes: Uint8Array = fromString(' ', 'utf8');
const worldBytes: Uint8Array = fromString('World', 'utf8');
const combinedBytes: Uint8Array = concat([helloBytes, spaceBytes, worldBytes]);
console.log('Concatenated bytes:', combinedBytes);

// Convert Uint8Array back to string
const decodedString: string = toString(combinedBytes, 'utf8');
console.log('Decoded string:', decodedString); // Expected: 'Hello World'

// Compare two Uint8Arrays for equality
const sameHelloBytes: Uint8Array = fromString('Hello', 'utf8');
const areEqual = equals(helloBytes, sameHelloBytes);
const areNotEqual = equals(helloBytes, worldBytes);
console.log('Are "Hello" and "Hello" equal?', areEqual); // Expected: true
console.log('Are "Hello" and "World" equal?', areNotEqual); // Expected: false

view raw JSON →