Skip to Content
Course content

240: Int8Array, Uint8Array, and Friends

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you’ve rented a massive, empty warehouse. This warehouse is just a giant, contiguous block of floor space. You can't "store" things in it directly because it's just a void; you first have to decide how to divide that floor. Do you put down a grid of tiny 1-foot squares for storing jewelry? Or do you mark out huge 10-foot zones for shipping crates? The space itself doesn't change, but the way you view and access it determines what fits and how much you can cram in there.

In JavaScript, that warehouse is the ArrayBuffer. It's a raw chunk of memory. But you can't actually manipulate an ArrayBuffer directly. To do anything useful, you need a "View"—and that's where Int8Array, Uint8Array, and their friends come in. They are the grids you lay over the warehouse floor to tell JavaScript, "Treat this block of memory as a sequence of 8-bit integers."

The Raw Space vs. The View

Most of the time, you'll just instantiate a TypedArray directly, and JavaScript handles the buffer creation behind the scenes. But it's important to understand the distinction if you're doing heavy lifting with binary data.

// This creates the "warehouse" (the buffer) and the "grid" (the view) at once.
const bytes = new Uint8Array(4); 
bytes[0] = 255;
bytes[1] = 0;
bytes[2] = 127;
bytes[3] = 64;

console.log(bytes.buffer); // This is the underlying ArrayBuffer

Notice I used Uint8Array. The "U" stands for Unsigned. This means it only handles positive numbers (0 to 255). If I had used Int8Array (Signed), I could store negative numbers, but my range would shift to -128 to 127. This is a crucial distinction when you're dealing with network packets or image pixels where a negative value would make zero sense.

Choosing Your Box Size

You've got a whole family of these views. The number (8, 16, 32) tells you how many bits each element takes up. If you're working with high-precision coordinates, an 8-bit integer is going to be uselessly small. That's when you bring out the big guns:

  • Int16Array / Uint16Array: 16 bits per element. Great for slightly larger integers.
  • Int32Array / Uint32Array: 32 bits. This is closer to what you're used to with standard JS numbers.
  • Float32Array / Float64Array: These are for decimals. Float64 is essentially the standard JavaScript Number type.

I'll be honest: you'll spend 90% of your time with Uint8Array because the web loves bytes. Whether you're using the Fetch API to get a blob or working with Canvas image data, everything boils down to 8-bit unsigned integers.

The Danger of Overflow

Here is where TypedArrays differ wildly from regular JavaScript arrays. Regular arrays are flexible; they grow and shrink, and their elements can be anything. TypedArrays are rigid. They are fixed-length, and they enforce their type strictly. If you try to put a number outside the allowed range into a Uint8Array, it doesn't throw an error—it wraps around.

const wrappingTest = new Uint8Array(1);
wrappingTest[0] = 256; 
console.log(wrappingTest[0]); // 0

wrappingTest[0] = 257;
console.log(wrappingTest[0]); // 1

It's like a clock. Once you hit 12, you go back to 1. This is called integer overflow. If you're building a game engine or a file parser, this can be a nightmare to debug if you aren't expecting it. Just keep in mind: these arrays are about performance and memory efficiency, not convenience.




📋 Practical Task

Build a Simple Binary Packet Parser

Imagine you are receiving data from a hardware temperature sensor. The sensor sends a "packet" of data as a raw ArrayBuffer. The packet structure is as follows:

  • The first 1 byte (Uint8) is the Sensor ID.
  • The next 2 bytes (Int16) represent the Temperature (multiplied by 100 to avoid decimals).
  • The final 1 byte (Uint8) is the Battery Level (0-100).

Your Task: Write a function called parseSensorData(buffer) that takes an ArrayBuffer as an argument and returns a human-readable object. You will need to use different "Views" (TypedArrays) to read the data from the same buffer.

// Test Case:
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setUint8(0, 42);          // Sensor ID: 42
view.setInt16(1, 2250);       // Temp: 22.50 degrees (2250 / 100)
view.setUint8(3, 85);          // Battery: 85%

const result = parseSensorData(buffer);
console.log(result); 
// Expected Output: { sensorId: 42, temperature: 22.5, battery: 85 }

Hint: While you can create multiple TypedArrays and point them at the same buffer using the offset parameter, the DataView object is often the cleanest way to read mixed types from a single buffer.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.