javascript ArrayBuffer, what's it for?

Viewed 48591

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

Can someone enlighten me?

Are there any uses anyone can think of that don't involve images?

3 Answers

An ArrayBuffer is a chunk of binary data in RAM. There are a few ways to "open" an ArrayBuffer for reading and writing:

  • Typed arrays, such as Uint16Array, can read and write the buffer by treating it as an array of integers. They don't let you control endianness; it uses the CPU's preferred endianness. Uint8Array is useful for controlling individual bytes (copying, slicing, etc).

  • DataView is not as simple, but it gives you more control. It lets you choose the endianness, integer size, and byte index (e.g. you can access a 32 bit integer at an index that's not divisible by 32 bits). These things can be specified each time you read and write an integer with the same DataView.

More info: https://javascript.info/arraybuffer-binary-arrays

Other than images, it's useful for precisely constructing and destructing low level network data packets used in protocols like UDP.

Related