Skip to main content
Bun provides full support for working with binary data through ArrayBuffer, TypedArrays, DataView, and Node.js Buffer.

ArrayBuffer

An ArrayBuffer is a fixed-length raw binary data buffer:

TypedArrays

TypedArrays provide a view into an ArrayBuffer:

Uint8Array

Other TypedArray Types

TypedArray Operations

DataView

DataView provides a low-level interface for reading/writing multiple number types in an ArrayBuffer:

Buffer (Node.js)

Buffer is a subclass of Uint8Array with additional methods:

Buffer Methods

Reading Numbers

Conversions

String ↔ Buffer

ArrayBuffer ↔ Buffer

TypedArray ↔ Buffer

Blob ↔ ArrayBuffer

Base64 Encoding/Decoding

Using Buffer

Using bun:base64

Hex Encoding/Decoding

SharedArrayBuffer

SharedArrayBuffer allows sharing memory between threads:

Crypto Random Bytes

Reading Binary Files

Writing Binary Files

Performance Tips

  1. Use Buffer.allocUnsafe() for temporary buffers - Faster but contains uninitialized data
  2. Use subarray() instead of slice() when possible - Creates a view instead of copying
  3. Reuse buffers - Avoid frequent allocations
  4. Use TypedArrays for specific data types - Better performance than DataView
  5. Use Buffer.concat() for combining buffers - More efficient than manual copying