Skip to main content
On this page

@std/bytes

Overview Jump to heading

Helper functions for working with Uint8Array byte slices.

import { concat, indexOfNeedle, endsWith } from "@std/bytes";
import { assertEquals } from "@std/assert";

const a = new Uint8Array([0, 1, 2]);
const b = new Uint8Array([3, 4, 5]);

const c = concat([a, b]);

assertEquals(c, new Uint8Array([0, 1, 2, 3, 4, 5]));

assertEquals(indexOfNeedle(c, new Uint8Array([2, 3])), 2);

assertEquals(endsWith(c, b), true);

Add to your project Jump to heading

deno add jsr:@std/bytes

See all symbols in @std/bytes on

What is a Uint8Array? Jump to heading

Uint8Array is a typed array view over an ArrayBuffer that represents raw bytes. Each element is an unsigned 8‑bit integer (0–255). Typed arrays are fixed‑length, efficient, and ideal for working with binary data from files, networks, crypto, images, WASM memory, and more.

When to use @std/bytes Jump to heading

If you are working with raw binary data (Uint8Array), for example searching, slicing, comparing or concatenating. This package provides helpful utility functions that are not built-in to JavaScript.

Examples Jump to heading

import { concat, equals, indexOfNeedle } from "@std/bytes";

const a = new Uint8Array([1, 2]);
const b = new Uint8Array([3, 4]);
const all = concat([a, b]); // Uint8Array [1,2,3,4]

console.log(equals(all, new Uint8Array([1, 2, 3, 4])));
console.log(indexOfNeedle(all, new Uint8Array([2, 3]))); // 1

Tips Jump to heading

  • Prefer these helpers over manual loops for clarity and correctness.
  • Convert text ↔ bytes using TextEncoder/TextDecoder.

Did you find what you needed?

Privacy policy