Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

Bytes unstable

Joins and compares byte sequences stored in Uint8Array values.

Byte-to-text conversions live in TextEncoder / TextDecoder, and hex or base64 conversions live in the Encoding modules.

3 exports Since v4.0.6 Source

Combinators

concat

unstable
Since v4.0.6

Joins byte chunks into one new Uint8Array.

When to use

Use to combine frames, headers, or streamed chunks into a single buffer.

Gotchas

The result is always a new array, even when there is zero or one chunk, so it never aliases an input.

Signature

declare function concat(chunks: readonly Array<Uint8Array<ArrayBufferLike>>): Uint8Array<ArrayBuffer>

Example Joining byte chunks

import { Bytes } from "effect"
const bytes = Bytes.concat([new Uint8Array([1, 2]), new Uint8Array([3])])
Array.from(bytes) // => [1, 2, 3]

Instances

Equivalence

unstable
Since v4.0.6

Compares byte sequences for equality, byte by byte.

Gotchas

This returns as soon as a byte differs, so it is not constant-time. Do not use it to compare secrets.

Signature

declare const Equivalence: Equ.Equivalence<Uint8Array>

Example Comparing byte sequences

import { Bytes } from "effect"
Bytes.Equivalence(new Uint8Array([1, 2]), new Uint8Array([1, 2])) // => true
Bytes.Equivalence(new Uint8Array([1, 2]), new Uint8Array([1])) // => false

Order

unstable
Since v4.0.6

Orders byte sequences lexicographically by unsigned byte value, placing a prefix before any longer sequence that starts with it.

Signature

declare const Order: order.Order<Uint8Array>

Example Sorting byte sequences

import { Bytes } from "effect"
Bytes.Order(new Uint8Array([1, 2]), new Uint8Array([1, 3])) // => -1
Bytes.Order(new Uint8Array([2]), new Uint8Array([1, 9])) // => 1
Bytes.Order(new Uint8Array([1]), new Uint8Array([1, 0])) // => -1