Sub topic 1
Why Binary Data in JavaScript
JavaScript started as a language for small scripts on web pages, and it worked mostly with strings, numbers, and objects. Modern JavaScript also decodes images, streams audio, talks to binary network protocols, and passes memory to WebAssembly. That work needs a way to handle raw bytes directly. This chapter explains why regular arrays fall short and introduces the three tools the rest of the tutorial covers.
Prerequisites
This chapter assumes you know:
- Core JavaScript syntax and regular arrays
- How JavaScript numbers work, including the difference between integers and floating point
- Hexadecimal notation (
0xff) and the idea of bits and bytes - Basic Promises and
fetch, which come up again in the I/O chapter
The problem with regular arrays
A regular Array can hold any value: numbers, strings, objects, or undefined. Every number is a 64-bit floating-point value. That flexibility is useful in general code. It gets in the way when the data is a sequence of bytes.
const bytes = [72, 105];
bytes.push(300); // not a valid byte, but nothing stops it
bytes.push('x'); // not even a number
console.log(bytes); // [ 72, 105, 300, 'x' ]Regular arrays cause three problems for binary work:
- No size or type guarantee. Nothing forces an element to be an integer from 0 to 255.
- Memory overhead. The engine may store each element as a full JavaScript value, which takes much more space than one byte.
- No shared layout with other systems. A file, a GPU buffer, or a WebAssembly module expects a contiguous block of bytes in a known format. A regular array has no such guaranteed layout.
A typed array fixes the first problem by converting every value to its element type when you write it:
const typed = new Uint8Array([72, 105, 300]);
console.log(typed); // Uint8Array(3) [ 72, 105, 44 ]The value 300 does not fit in a byte, so it wraps to 44 (300 − 256). A later chapter explains these conversion rules.
Where binary data shows up
| Area | Example | What the bytes represent |
|---|---|---|
| Files | Reading a PNG or ZIP the user picked | Headers, compressed data |
| Network | `fetch` responses, WebSocket frames | Protocol messages |
| Graphics | WebGL vertex buffers, canvas pixels | Floats, RGBA bytes |
| Audio | Web Audio sample buffers | 32-bit float samples |
| WebAssembly | Linear memory | The module's whole heap |
| Workers | Passing large data between threads | Any of the above |
The three building blocks
JavaScript separates storage from interpretation:
ArrayBufferis a fixed-length block of raw bytes. You cannot read it directly.- Typed arrays such as
Uint8ArrayandFloat32Arrayare views that read the buffer as a list of numbers of one type. DataViewis a view that reads and writes values of any type at any byte offset, with a byte order you choose.
Several views can look at the same buffer at the same time. When you write through one view, the others see the change. Much of this tutorial covers when that sharing helps and when it causes bugs.
Common misconceptions
- "Typed arrays are just faster arrays." They hold one numeric type and have a fixed length. They have no
pushorpop. - "An ArrayBuffer is an array." You can't index into it. You always need a view.
- "Bytes and characters are the same." In UTF-8, one character can take several bytes.
TextEncoderhandles the conversion.
Exercise
You need to store 1,000,000 audio samples as 32-bit floats and send them to a Web Audio API. Why is a regular Array a poor fit, and which building blocks would you use?
Show answer
A regular Array doesn't guarantee that every element is a number, stores each value as a full JavaScript value with extra memory overhead, and has no contiguous byte layout that the API can use. Use an ArrayBuffer of 4,000,000 bytes viewed through a Float32Array, or simply create a Float32Array of length 1,000,000, which allocates its own buffer.
Exercise
An ArrayBuffer and a Uint8Array view over it both exist. Which one do you index into to read the third byte, and why?
Show answer
You index into the Uint8Array (view[2]). An ArrayBuffer only holds raw bytes and has no indexed access. A view is needed to interpret those bytes.