# uint8arrays

> Utility functions to make dealing with Uint8Arrays easier

Latest version **6.1.1** (published 2026-05-07) · Apache-2.0 OR MIT license · 0 weekly downloads

## Install

```sh
npm install uint8arrays
pnpm add uint8arrays
yarn add uint8arrays
bun add uint8arrays
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.1.1 |
| Published | 2026-05-07 |
| First published | 2020-07-31 |
| Weekly downloads | 0 |
| License | Apache-2.0 OR MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 180.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 48 |
| Author | Alex Potsides |
| Maintainers | achingbrain |

## Links

- npm: https://www.npmjs.com/package/uint8arrays
- Repository: https://github.com/achingbrain/uint8arrays
- Homepage: https://github.com/achingbrain/uint8arrays#readme
- Issues: https://github.com/achingbrain/uint8arrays/issues
- npm.io page: https://npm.io/package/uint8arrays

## Dependencies (1)

- [multiformats](https://npm.io/package/multiformats.md) ^14.0.0

## Recent versions

- 6.1.1 (latest) — 2026-05-07
- 3.0.0-rc.1 (next) — 2021-08-16
- 6.1.0 — 2026-05-07
- 6.0.0 — 2026-05-07
- 5.1.1 — 2026-04-13
- 5.1.0 — 2024-05-10
- 5.0.3 — 2024-03-13
- 5.0.2 — 2024-02-08
- 5.0.1 — 2023-12-28
- 5.0.0 — 2023-12-07
- 4.0.10 — 2023-12-07
- 4.0.9 — 2023-11-24
- 4.0.8 — 2023-11-24
- 4.0.7 — 2023-11-24
- 4.0.6 — 2023-08-03
- … 32 more at https://npm.io/package/uint8arrays/versions

## README

# uint8arrays

[![codecov](https://img.shields.io/codecov/c/github/achingbrain/uint8arrays.svg?style=flat-square)](https://codecov.io/gh/achingbrain/uint8arrays)
[![CI](https://img.shields.io/github/actions/workflow/status/achingbrain/uint8arrays/js-test-and-release.yml?branch=main\&style=flat-square)](https://github.com/achingbrain/uint8arrays/actions/workflows/js-test-and-release.yml?query=branch%3Amain)

> Utility functions to make dealing with Uint8Arrays easier

# About

<!--

!IMPORTANT!

Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.

To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts

To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.

-->

`Uint8Array`s bring memory-efficient(ish) byte handling to browsers - they are similar to Node.js `Buffer`s but lack a lot of the utility methods present on that class.

This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc.

Since Node.js `Buffer`s are also `Uint8Array`s, it falls back to `Buffer` internally where it makes sense for performance reasons.

## alloc(size)

Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`.

## Example

```ts
import { alloc } from 'uint8arrays/alloc'

const buf = alloc(100)
```

## allocUnsafe(size)

Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`.

On platforms that support it, memory referenced by the returned `Uint8Array` will not be initialized.

## Example

```ts
import { allocUnsafe } from 'uint8arrays/alloc'

const buf = allocUnsafe(100)
```

## compare(a, b)

Compare two `Uint8Arrays`

## Example

```ts
import { compare } from 'uint8arrays/compare'

const arrays = [
  Uint8Array.from([3, 4, 5]),
  Uint8Array.from([0, 1, 2])
]

const sorted = arrays.sort(compare)

console.info(sorted)
// [
//    Uint8Array[0, 1, 2]
//    Uint8Array[3, 4, 5]
// ]
```

## concat(arrays, \[length])

Concatenate one or more `Uint8Array`s and return a `Uint8Array` with their contents.

If you know the length of the arrays, pass it as a second parameter, otherwise it will be calculated by traversing the list of arrays.

## Example

```ts
import { concat } from 'uint8arrays/concat'

const arrays = [
  Uint8Array.from([0, 1, 2]),
  Uint8Array.from([3, 4, 5])
]

const all = concat(arrays, 6)

console.info(all)
// Uint8Array[0, 1, 2, 3, 4, 5]
```

## equals(a, b)

Returns true if the two arrays are the same array or if they have the same length and contents.

## Example

```ts
import { equals } from 'uint8arrays/equals'

const a = Uint8Array.from([0, 1, 2])
const b = Uint8Array.from([3, 4, 5])
const c = Uint8Array.from([0, 1, 2])

console.info(equals(a, b)) // false
console.info(equals(a, c)) // true
console.info(equals(a, a)) // true
```

## fromString(string, encoding = 'utf8')

Returns a new `Uint8Array` created from the passed string and interpreted as the passed encoding.

Supports `utf8` and any of the [multibase encodings](https://github.com/multiformats/multibase/blob/master/multibase.csv) as implemented by the [multiformats module](https://www.npmjs.com/package/multiformats).

## Example

```ts
import { fromString } from 'uint8arrays/from-string'

console.info(fromString('hello world')) // Uint8Array[104, 101 ...
console.info(fromString('00010203aabbcc', 'base16')) // Uint8Array[0, 1 ...
console.info(fromString('AAECA6q7zA', 'base64')) // Uint8Array[0, 1 ...
console.info(fromString('01234', 'ascii')) // Uint8Array[48, 49 ...
```

## toString(array, encoding = 'utf8')

Returns a string created from the passed `Uint8Array` in the passed encoding.

Supports `utf8` and any of the [multibase encodings](https://github.com/multiformats/multibase/blob/master/multibase.csv) as implemented by the [multiformats module](https://www.npmjs.com/package/multiformats).

## Example

```ts
import { toString } from 'uint8arrays/to-string'

console.info(toString(Uint8Array.from([104, 101...]))) // 'hello world'
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base16')) // '00010203aabbcc'
console.info(toString(Uint8Array.from([0, 1, 2...]), 'base64')) // 'AAECA6q7zA'
console.info(toString(Uint8Array.from([48, 49, 50...]), 'ascii')) // '01234'
```

## withArrayBuffer(buf)

`Uint8Array`s can be backed by an `ArrayBuffer` or a `SharedArrayBuffer`.

The types of these two values have diverged in `typescript@5.7` or later.

Some APIs require `Uint8Array`s backed by `ArrayBuffer`s specifically so call
`withArrayBuffer` to ensure your `Uint8Array`s are backed the correct type.

## Example

```ts
import { withArrayBuffer } from 'uint8arrays/with-array-buffer'

const buf = new SharedArrayBuffer(10)
const arr = new Uint8Array(buf, 0, buf.byteLength)

const arr2 = withArrayBuffer(arr)

console.info(arr.buffer.constructor) // [Function: SharedArrayBuffer]
console.info(arr2.buffer.constructor) // [Function: ArrayBuffer]
```

## xor(a, b)

Returns a `Uint8Array` containing `a` and `b` xored together.

## Example

```ts
import { xor } from 'uint8arrays/xor'

console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1]
```

## xorCompare(a, b)

Compares the distances between two xor `Uint8Array`s.

## Example

```ts
import { xor } from 'uint8arrays/xor'
import { xorCompare } from 'uint8arrays/xor-compare'

const target = Uint8Array.from([1, 1])
const val1 = Uint8Array.from([1, 0])
const xor1 = xor(target, val1)

const val2 = Uint8Array.from([0, 1])
const xor2 = xor(target, val2)

console.info(xorCompare(xor1, xor2)) // -1 or 0 or 1
```

# Install

```console
$ npm i uint8arrays
```

## Browser `<script>` tag

Loading this module through a script tag will make its exports available as `Uint8arrays` in the global namespace.

```html
<script src="https://unpkg.com/uint8arrays/dist/index.min.js"></script>
```

# API Docs

- <https://achingbrain.github.io/uint8arrays>

# License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](https://github.com/achingbrain/uint8arrays/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/achingbrain/uint8arrays/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

# Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

---
_Source: https://npm.io/package/uint8arrays · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
