Skip to main content
This page is intended as an introduction to working with binary data in JavaScript. Bun implements several data types and utilities for working with binary data, most of which are Web-standard. Any Bun-specific APIs will be noted as such. Below is a cheat sheet that doubles as a table of contents. Click an item in the left column to jump to that section.

ArrayBuffer and views

Until 2009, there was no language-native way to store and manipulate binary data in JavaScript. ECMAScript v5 introduced a range of new mechanisms for this. The most fundamental building block is ArrayBuffer, a data structure that represents a sequence of bytes in memory.
Despite the name, it isn’t an array and supports none of the array methods and operators one might expect. In fact, there is no way to directly read or write values from an ArrayBuffer. There’s very little you can do with one except check its size and create “slices” from it.
To do anything interesting we need a construct known as a “view”. A view is a class that wraps an ArrayBuffer instance and lets you read and manipulate the underlying data. There are two types of views: typed arrays and DataView.

DataView

The DataView class is a lower-level interface for reading and manipulating the data in an ArrayBuffer. Below we create a new DataView and set the first byte to 3.
Now let’s write a Uint16 at byte offset 1. This requires two bytes. We’re using the value 513, which is 2 * 256 + 1; in bytes, that’s 00000010 00000001.
We’ve now assigned a value to the first three bytes in our underlying ArrayBuffer. Even though the second and third bytes were created using setUint16(), we can still read each of its component bytes using getUint8().
Attempting to write a value that requires more space than is available in the underlying ArrayBuffer will cause an error. Below we attempt to write a Float64 (which requires 8 bytes) at byte offset 0, but there are only four total bytes in the buffer.
The following methods are available on DataView:

TypedArray

Typed arrays are a family of classes that provide an Array-like interface for interacting with data in an ArrayBuffer. Whereas a DataView lets you write numbers of varying size at a particular offset, a TypedArray interprets the underlying bytes as an array of numbers, each of a fixed size.
It’s common to refer to this family of classes collectively by their shared superclass TypedArray. This class as internal to JavaScript; you can’t directly create instances of it, and TypedArray is not defined in the global scope. Think of it as an interface or an abstract class.
While an ArrayBuffer is a generic sequence of bytes, these typed array classes interpret the bytes as an array of numbers of a given byte size. The top row contains the raw bytes, and the later rows contain how these bytes will be interpreted when viewed using different typed array classes. The following classes are typed arrays, along with a description of how they interpret the bytes in an ArrayBuffer: Here’s the first table formatted as a markdown table: The table below demonstrates how the bytes in an ArrayBuffer are interpreted when viewed using different typed array classes. To create a typed array from a pre-defined ArrayBuffer:
If we tried to instantiate a Uint32Array from this same ArrayBuffer, we’d get an error.
A Uint32 value requires four bytes (16 bits). Because the ArrayBuffer is 10 bytes long, there’s no way to cleanly divide its contents into 4-byte chunks. To fix this, we can create a typed array over a particular “slice” of an ArrayBuffer. The Uint16Array below only “views” the first 8 bytes of the underlying ArrayBuffer. To achieve these, we specify a byteOffset of 0 and a length of 2, which indicates the number of Uint32 numbers we want our array to hold.
You don’t need to explicitly create an ArrayBuffer instance; you can instead directly specify a length in the typed array constructor:
Typed arrays can also be instantiated directly from an array of numbers, or another typed array:
Broadly speaking, typed arrays provide the same methods as regular arrays, with a few exceptions. For example, push and pop are not available on typed arrays, because they would require resizing the underlying ArrayBuffer.
Refer to the MDN documentation for more information on the properties and methods of typed arrays.

Uint8Array

It’s worth specifically highlighting Uint8Array, as it represents a classic “byte array”—a sequence of 8-bit unsigned integers between 0 and 255. This is the most common typed array you’ll encounter in JavaScript. In Bun, and someday in other JavaScript engines, it has methods available for converting between byte arrays and serialized representations of those arrays as base64 or hex strings.
It is the return value of TextEncoder#encode, and the input type of TextDecoder#decode, two utility classes designed to translate strings and various binary encodings, most notably "utf-8".

Buffer

Bun implements Buffer, a Node.js API for working with binary data that pre-dates the introduction of typed arrays in the JavaScript spec. It has since been re-implemented as a subclass of Uint8Array. It provides a wide range of methods, including several Array-like and DataView-like methods.
For complete documentation, refer to the Node.js documentation.

Blob

Blob is a Web API commonly used for representing files. Blob was initially implemented in browsers (unlike ArrayBuffer which is part of JavaScript itself), but it is now supported in Node and Bun. It isn’t common to directly create Blob instances. More often, you’ll receive instances of Blob from an external source (like an <input type="file"> element in the browser) or library. That said, it is possible to create a Blob from one or more string or binary “blob parts”.
These parts can be string, ArrayBuffer, TypedArray, DataView, or other Blob instances. The blob parts are concatenated together in the order they are provided.
The contents of a Blob can be asynchronously read in various formats.

BunFile

BunFile is a subclass of Blob used to represent a lazily-loaded file on disk. Like File, it adds a name and lastModified property. Unlike File, it does not require the file to be loaded into memory.

File

Browser only. Experimental support in Node.js 20.
File is a subclass of Blob that adds a name and lastModified property. It’s commonly used in the browser to represent files uploaded via a <input type="file"> element. Node.js and Bun implement File.
Refer to the MDN documentation for complete docs information.

Streams

Streams are an important abstraction for working with binary data without loading it all into memory at once. They are commonly used for reading and writing files, sending and receiving network requests, and processing large amounts of data. Bun implements the Web APIs ReadableStream and WritableStream.
Bun also implements the node:stream module, including Readable, Writable, and Duplex. For complete documentation, refer to the Node.js docs.
To create a readable stream:
The contents of this stream can be read chunk-by-chunk with for await syntax.
For a more complete discussion of streams in Bun, see API > Streams.

Conversion

Converting from one binary format to another is a common task. This section is intended as a reference.

From ArrayBuffer

Since ArrayBuffer stores the data that underlies other binary structures like TypedArray, the snippets below are not converting from ArrayBuffer to another format. Instead, they are creating a new instance using the data stored underlying data.

To TypedArray

To DataView

To Buffer

To string

As UTF-8:

To number[]

To Blob

To ReadableStream

The following snippet creates a ReadableStream and enqueues the entire ArrayBuffer as a single chunk.
To stream the ArrayBuffer in chunks, use a Uint8Array view and enqueue each chunk.

From TypedArray

To ArrayBuffer

This retrieves the underlying ArrayBuffer. Note that a TypedArray can be a view of a slice of the underlying buffer, so the sizes may differ.

To DataView

To creates a DataView over the same byte range as the TypedArray.

To Buffer

To string

As UTF-8:

To number[]

To Blob

To ReadableStream

To stream the ArrayBuffer in chunks, split the TypedArray into chunks and enqueue each one individually.

From DataView

To ArrayBuffer

To TypedArray

Only works if the byteLength of the DataView is a multiple of the BYTES_PER_ELEMENT of the TypedArray subclass.

To Buffer

To string

As UTF-8:

To number[]

To Blob

To ReadableStream

To stream the ArrayBuffer in chunks, split the DataView into chunks and enqueue each one individually.

From Buffer

To ArrayBuffer

To TypedArray

To DataView

To string

As UTF-8:
As base64:
As hex:

To number[]

To Blob

To ReadableStream

To stream the ArrayBuffer in chunks, split the Buffer into chunks and enqueue each one individually.

From Blob

To ArrayBuffer

The Blob class provides a convenience method for this purpose.

To TypedArray

To DataView

To Buffer

To string

As UTF-8:

To number[]

To ReadableStream

From ReadableStream

It’s common to use Response as a convenient intermediate representation to make it easier to convert ReadableStream to other formats.
However this approach is verbose and adds overhead that slows down overall performance unnecessarily. Bun implements a set of optimized convenience functions for converting ReadableStream various binary formats.

To ArrayBuffer

To Uint8Array

To TypedArray

To DataView

To Buffer

To string

As UTF-8:

To number[]

Bun provides a utility for resolving a ReadableStream to an array of its chunks. Each chunk may be a string, typed array, or ArrayBuffer.

To Blob

To ReadableStream

To split a ReadableStream into two streams that can be consumed independently: