ArrayBuffer & Binary Arrays in JavaScript
What is ArrayBuffer
?
ArrayBuffer
is a low-level binary data structure that allows you to store raw binary data in memory. It is useful when dealing with WebSockets, File APIs, or binary data processing (e.g., images, audio, and video).
1. Understanding ArrayBuffer
An ArrayBuffer
represents a fixed-length chunk of memory that can be viewed and manipulated using typed arrays like Uint8Array
, Float32Array
, etc.
Creating an ArrayBuffer
🔹 Doesn't store data directly—you need a view (TypedArray) to manipulate it.
2. Working with Typed Arrays (Uint8Array
, Float32Array
, etc.)
Typed arrays allow you to interpret an ArrayBuffer
as a specific data type.
Writing Data to an ArrayBuffer
🔹 Uint8Array
stores each element in 1 byte (0-255).
🔹 300
is truncated because Uint8Array
supports only 0 to 255 (300 % 256 = 44
).
3. Typed Array Variants
TypedArray | Size per element | Range |
---|---|---|
Int8Array | 1 byte | -128 to 127 |
Uint8Array | 1 byte | 0 to 255 |
Uint8ClampedArray | 1 byte | 0 to 255 (values > 255 are set to 255) |
Int16Array | 2 bytes | -32,768 to 32,767 |
Uint16Array | 2 bytes | 0 to 65,535 |
Int32Array | 4 bytes | -2,147,483,648 to 2,147,483,647 |
Uint32Array | 4 bytes | 0 to 4,294,967,295 |
Float32Array | 4 bytes | Floating-point numbers |
Float64Array | 8 bytes | High-precision floating points |
Example: Using a Float32Array
🔹 Float32Array
stores 32-bit floating-point numbers.
4. Reading Binary Data with DataView
DataView
allows more precise control over binary data, letting you read/write different types at different offsets.
Example: Using DataView
🔹 setInt16(0, 42)
stores 42 as a 16-bit integer at byte 0.
🔹 setFloat32(2, 3.14)
stores 3.14 as a 32-bit float at byte 2.
5. Converting ArrayBuffer
to String (Text Encoding)
Using TextDecoder
and TextEncoder
Useful for encoding/decoding binary data into strings.
Encoding a String into an ArrayBuffer
🔹 Converts "Hello"
into its UTF-8 binary representation.
Decoding an ArrayBuffer
into a String
🔹 Converts binary data back into a string.
6. Real-World Applications of ArrayBuffer
📌 1. Handling Files (FileReader
)
Read a file as an ArrayBuffer
:
🔹 Converts a file into binary data.
📌 2. WebSockets (Binary Data Transfer)
Send and receive binary data efficiently.
🔹 Transfers binary data over a WebSocket connection.
📌 3. Processing Images
Using Canvas
to convert an image to ArrayBuffer
:
🔹 Converts an image into raw binary data for processing.
🔹 Summary: Why Use ArrayBuffer
?
Feature | ArrayBuffer |
---|---|
Binary Data | ✅ Yes |
Fast Performance | ✅ Yes |
Manipulate Memory | ✅ Yes |
Supports Large Data | ✅ Yes |
Used in Web APIs | ✅ Yes |
ArrayBuffer
is essential for performance-critical applications like handling binary data, WebSockets, image processing, and file manipulation.
🎯 Conclusion
✅ ArrayBuffer
stores raw binary data in memory.
✅ Typed Arrays (Uint8Array
, Float32Array
, etc.) view and manipulate the buffer.
✅ DataView
allows precise control over binary data.
✅ TextEncoder
and TextDecoder
help with text encoding.
✅ Real-world use cases: File reading, WebSockets, image processing, etc.
🔹 Need help implementing ArrayBuffer
? Let me know!