This is the 28th day of my participation in the August Text Challenge.More challenges in August

ArrayBuffer

An ArrayBuffer object is an interface for JavaScript to manipulate binary data. As separate specifications (released in February 2011), ES6 incorporates them into the ECMAScript specification. It handles binary data in the syntax of an array, so it is called a binary array.

Binary arrays were born in this context. It allows developers to manipulate memory directly in the form of array subscripts, greatly enhancing JavaScript’s ability to handle binary data.

A binary array consists of three types of objects: ArrayBuffer, TypedArray, and DataView. The ArrayBuffer represents raw binary data. The TypedArray view reads and writes simple binary data. The DataView is used to read and write binary data of complex types.

ArrayBuffer object

An ArrayBuffer represents a chunk of memory that stores binary data. It cannot be read or written directly, but can only be read or written through views (TypedArray and DataView views) that interpret binary data in a specified format.

An ArrayBuffer is also a constructor that allocates a contiguous area of memory where data can be stored. In a 32-byte memory area, the default value for each byte is 0. As you can see, the argument to the ArrayBuffer constructor is the required memory (bytes).

const buf = new ArrayBuffer(32);
Copy the code

DataView creation requires an ArrayBuffer object instance as an argument. Create the DataView, and then read the 8-bit binary data from scratch in unsigned 8-bit integer format, resulting in 0 because all bits are 0 by default for the ArrayBuffer object in raw memory.

const buf = new ArrayBuffer(32);
const dataView = new DataView(buf);
dataView.getUint8(0)
Copy the code

A TypedArray view, which differs from a DataView view to represent a different data format, is not a constructor but a set of constructors. They are 32-bit signed integers (Int32Array constructor) and 8-bit unsigned integers (Uint8Array constructor). Since the two views correspond to the same memory segment, the modification of the underlying memory by one view affects the other view.

const buffer = new ArrayBuffer(12);
const x1 = new Int32Array(buffer);
x1[0] = 1;
const x2 = new Uint8Array(buffer);
x2[0]  = 2;
x1[0]
Copy the code

ArrayBuffer.isView()

ArrayBuffer has a static method, isView, that returns a Boolean value indicating whether the arguments are view instances of ArrayBuffer. This method is roughly equivalent to determining whether the parameter is an instance of TypedArray or DataView.

const buffer = new Int32Array(buffer);
ArrayBuffer.isView(buffer)
Copy the code

ArrayBuffer.prototype.slice()

An ArrayBuffer instance has a slice method that allows a new ArrayBuffer object to be copied from a portion of the memory area.

const buffer = new ArrayBuffer(8);
const newBuffer = buffer.slice(0, 3);
Copy the code

The slice method takes two arguments, the first representing the byte sequence number at the start of the copy (inclusive) and the second representing the byte sequence number at the end of the copy (inclusive). If the second argument is omitted, it defaults to the end of the original ArrayBuffer object.

In addition to the slice method, ArrayBuffer objects do not provide any means of reading or writing directly to memory, only allowing views to be created on top of them and then read from them.

ArrayBuffer.prototype.byteLength

The byteLength property of an ArrayBuffer instance that returns the size of the allocated memory region in bytes.

const buffer = new ArrayBuffer(32);
buffer.byteLength
// 32
Copy the code

If the area of memory to be allocated is large, it is possible that the allocation will fail (because there is not that much contiguous free memory), so it is necessary to check whether the allocation is successful.