What is ArrayBuffer in JavaScript

Javascript ArrayBuffer, what is it for?


Maybe it's late, or maybe it's why, but I've just read the documents for ArrayBuffer and can't think of a single thing that it would be really useful for.

Can someone enlighten me?

Are there any uses that anyone can remember that aren't pictures?





Reply:


Basically, ArrayBuffer is used to store binary data. This can be the binary data of an image, for example.

Buffers have proven very useful in other languages. Yes, of course it is not as difficult to understand / use as other data types.

ArrayBuffer can be used to get data from JPG (RGB Bytes) images and create a PNG from them by adding Alpha Bytes (i.e. RGBA).

The Mozilla site made limited use of ArrayBuffer here

Working with complex data structures

By combining a single buffer with multiple views of different types, starting with different offsets in the buffer, you can interact with data objects that contain multiple data types. This allows you to interact with, for example, complex data structures from WebGL, data files, or C structures that you must use when using js-ctypes.

Consider this C structure:

You can access a buffer that contains data in this format:

Then you can, for example, access the amount due.

Note: The alignment of the data structure in a C structure is platform-dependent. Take precautions and considerations for these padding differences.






Other than images, it is useful for precisely creating and destroying low-level network data packets used in protocols like UDP.


An represents binary data in RAM. You can read and write one with either a typed array or with an "open".

Typed arrays such as can read and write to the buffer by treating it as a single array of integers. They don't let you control the endianness; It uses the native endianness of the platform. Its use is useful for controlling individual bytes (this is not affected by endianness).

is not that simple, but it gives you a lot more control. You have full control over endianness, integer size and byte index (e.g. you can access a 32-bit integer at an index, even if it is not divisible by 32 bits). These things can be selected every time you read and write an integer with the same.

This can be helpful: https://javascript.info/arraybuffer-binary-arrays

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.