Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 133 ยท Buffers and Binary Data

Typed Arrays, ArrayBuffer and DataView

Learn typed arrays, ArrayBuffer and DataView in Node.js - element types, views and shared memory, endianness, resizable and transferable buffers, SharedArrayBuffer and parsing binary file formats.

Ordinary JavaScript arrays are flexible: they can hold numbers, strings and objects together and grow at will. That flexibility costs memory and speed. When you process an image, an audio signal, a million sensor readings or a binary file format, you want something stricter: a block of memory in which every element has the same size and type. JavaScript calls this a typed array, and it is the standard foundation for binary data in browsers and in Node.js alike.

Typed arrays come in three layers. An ArrayBuffer is the raw block of bytes, with no way to read it directly. A view such as Uint8Array or Float64Array interprets the bytes as a sequence of numbers of one type. A DataView is a flexible view that reads any type at any byte offset with a chosen byte order. The Buffer class from the previous lesson is just a Uint8Array with extra methods, so everything here applies to it, too.

ArrayBuffer: the raw memory

new ArrayBuffer(n) allocates n zeroed bytes. You cannot index it; you only see its byteLength. All reading and writing goes through views.

Node.js One buffer, several views

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

All three views look at the same memory. Writing a byte through bytes changes what words sees, and writing the double 1 through one fills the bytes with its IEEE 754 representation (00 00 00 00 00 00 f0 3f on a little-endian machine). Each view has length (number of elements), byteLength and buffer; byteOffset tells where in the buffer the view begins.

The typed array family

Constructor Element Bytes Range or notes
Int8Array signed integer 1 -128 to 127
Uint8Array unsigned integer 1 0 to 255
Uint8ClampedArray unsigned integer 1 0 to 255, values clamp instead of wrap
Int16Array / Uint16Array integer 2 -32768..32767 / 0..65535
Int32Array / Uint32Array integer 4 about +-2.1 billion / 4.29 billion
Float32Array floating point 4 about 7 significant digits
Float64Array floating point 8 the same as a JavaScript number
BigInt64Array / BigUint64Array BigInt 8 64-bit integers as BigInt

The elements are stored as the stated type, so assignments convert and may lose information:

Node.js Wrapping, clamping and precision

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Uint8ClampedArray exists for pixel data, where 300 should become 255. Float32Array cannot hold 0.1 exactly; it stores the nearest 32-bit value. 1e40 is too large for a 32-bit float, so it becomes Infinity. Choose the smallest type that is large enough: a million Float32 values use 4 MB, a million regular numbers in an array use roughly twice as much or more, and the typed array is also faster to iterate because the memory is contiguous.

Creating typed arrays

There are several constructor forms. They differ in whether they allocate new memory or reuse existing memory.

Node.js Ways to create a typed array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The (buffer, byteOffset, length) form is the one that matters for binary formats. A common error is an unaligned offset: a Uint32Array view must start at a multiple of 4, and the length of the buffer must fit the element size. When you cannot guarantee alignment (for instance a field in the middle of a file), use DataView, described below.

Working with the elements

Typed arrays have most of the array methods: map, filter, reduce, slice, indexOf, includes, join, sort (numeric by default, unlike arrays), at, find, toSorted, toReversed and with. They do not have push, pop, splice or anything else that changes the length.

Node.js Typed array methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Two behaviours to remember: sort() on a typed array compares numbers (a Uint8Array([10, 9, 1]) sorts to 1, 9, 10), whereas on an ordinary array it compares text. And, just as for buffers, subarray returns a view and slice returns a copy.

Endianness

A 16-bit or larger number takes several bytes, and there are two ways to order them. Big endian puts the most significant byte first (0x1234 is 12 34), the order used in network protocols and many file formats. Little endian puts it last (34 12), what x86 and ARM processors use in memory. Typed array views use the host's byte order, which is a trap for files: the same file would read differently on a big-endian machine. Today virtually every machine is little endian, but code that reads a defined format should not depend on that.

Node.js Detecting byte order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The last two lines show the problem. The bytes 12 34 are the big-endian representation of 0x1234, but a Uint16Array on a little-endian host reads them as 0x3412. The fix is DataView.

DataView: any type, any offset, any byte order

A DataView wraps an ArrayBuffer (or a part of it) and offers getInt8, getUint16, getUint32, getFloat32, getFloat64, getBigInt64 and matching set methods. Each takes a byte offset, and the multi-byte ones take an optional littleEndian flag. The default is big endian, the reverse of typed arrays, which is another point to remember.

Node.js Reading with explicit endianness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Use typed arrays when you process many elements of one type (they are fast and have array methods). Use DataView when you parse a structure with mixed fields, unaligned offsets or a fixed byte order. Node's Buffer also has readUInt16BE and similar methods that cover the same ground, with a slightly different style.

Parsing a binary file format

Here is a complete example: a tiny made-up image format. The header is 8 bytes (a 4-byte magic number TINY, a 16-bit width and a 16-bit height, both big endian), followed by one byte per pixel.

Node.js Writing and parsing a small binary format

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice the habits that make binary code safe: check the magic number first, read the header with a DataView and explicit byte order, and create the pixel view over the same memory without copying. A production parser also checks that the buffer is long enough for width * height before slicing, since a hostile file can claim a huge size. Typed array constructors throw a RangeError when the claimed range does not fit, which is a safety net but not a substitute for validation.

Resizable and transferable buffers

Modern JavaScript added two features for large data. They are available in Node 24; they came in during the Node 20 to 22 series, so older runtimes may lack them.

A resizable ArrayBuffer grows (up to a maximum you declare) without being replaced, and views created without a fixed length follow its size.

Node.js Resizable ArrayBuffer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

When the buffer shrinks below a fixed-length view, that view becomes "out of bounds" and reports length 0 until the buffer grows again; length-tracking views just follow. Use resizable buffers for append-style data (a growing receive buffer) instead of reallocating and copying.

A transferable ArrayBuffer can hand over its memory in one step: buffer.transfer() returns a new buffer with the same contents and detaches the original, which then has length 0 and cannot be used. This is what makes passing big binary data to a worker cheap; see Worker Threads.

Node.js transfer and detached buffers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

After a transfer the old views report zero length: source.length is 0 in the last line. Detaching prevents two owners from changing the same memory.

SharedArrayBuffer and Atomics

A SharedArrayBuffer is memory that several threads can see at once; it is not copied when posted to a worker. Because threads can then read and write at the same time, plain operations are racy, and the Atomics object provides safe operations (Atomics.add, Atomics.compareExchange, Atomics.wait and notify). Here is a short preview; workers get their own lesson.

Node.js SharedArrayBuffer and Atomics preview

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Three workers each add 1,000 times to the same integer, and Atomics.add guarantees that no increment is lost, so the total is always 3000. With a plain counter[0]++ the result would vary from run to run because two threads can read the same old value. Shared memory is the fastest way to communicate between threads and the hardest to get right; prefer message passing unless you have measured a need.

Choosing between the tools

You need... Use
Raw bytes with text encodings (hex, base64), files and sockets Buffer
A numeric array of one type, processed in bulk Uint8Array, Float32Array, ...
A structure with mixed fields, unaligned offsets, a specified byte order DataView
Memory shared between threads SharedArrayBuffer + Atomics
Growing a binary buffer without copying resizable ArrayBuffer
Moving a large buffer to a worker cheaply transfer() or a transfer list

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the byte order used by DataView methods when you omit the littleEndian argument?

    DataView defaults to big endian. Typed arrays use the host order, which is little endian on nearly all machines.
  2. 2. What does new Uint8Array([300, -1]) contain?

    Uint8Array wraps modulo 256: 300 becomes 44 and -1 becomes 255. Uint8ClampedArray would give 255 and 0.
  3. 3. Two typed arrays are created on the same ArrayBuffer. One of them writes to index 0. What does the other see?

    Views do not own data; they are windows onto the ArrayBuffer.
  4. 4. How does sort() on a typed array differ from sort() on a normal array without a comparator?

    TypedArray.prototype.sort compares numbers by default, so 10 comes after 9.
  5. 5. What happens to the original ArrayBuffer after const moved = original.transfer()?

    Transfer moves ownership of the memory. The old ArrayBuffer is detached, and views onto it report length 0.

Summary

  • An ArrayBuffer is raw memory; typed arrays and DataView are views that give it meaning, and views on the same buffer share memory.
  • Typed arrays hold elements of a single numeric type and convert on assignment (wrapping, clamping, precision loss).
  • slice copies and subarray shares; typed arrays have array methods except those that change the length.
  • Typed arrays use the host byte order, DataView defaults to big endian and accepts a flag; use DataView for file and network formats.
  • Resizable buffers grow in place, transfer() moves ownership, and SharedArrayBuffer with Atomics shares memory between threads.
  • Validate lengths and magic numbers when parsing untrusted binary data.

Next: Text, Encodings and Unicode