'javascript ArrayBuffer, what's it for?
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?
Solution 1:[1]
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.Uint8Arrayis useful for controlling individual bytes (copying, slicing, etc).DataViewis 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 sameDataView.
More info: https://javascript.info/arraybuffer-binary-arrays
Solution 2:[2]
Other than images, it's useful for precisely constructing and destructing low level network data packets used in protocols like UDP.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jonathan |
