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?
Interface ArrayBufferRepresents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly, but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.
1. A Buffer is just a view for looking into an ArrayBuffer . A Buffer , in fact, is a FastBuffer , which extends (inherits from) Uint8Array , which is an octet-unit view (“partial accessor”) of the actual memory, an ArrayBuffer .
JavaScript typed arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers. Array objects grow and shrink dynamically and can have any JavaScript value. JavaScript engines perform optimizations so that these arrays are fast.
An ArrayBuffer is in the memory, available for manipulation. A Blob can be on disk, in cache memory, and other places not readily available.
Basically ArrayBuffer is used to keep binary data. It can be the binary data of an image for example.
In other languages buffers are proved very useful. Yes, of-course it is little difficult to understand/use than other data types.
ArrayBuffer can be used to get data of jpg image (RGB bytes) and produce a png out of it by adding alpha byte (i.e. RGBA).
Mozilla site has given a small use of ArrayBuffer here
Working with complex data structures
By combining a single buffer with multiple views of different types, starting at different offsets into the buffer, you can interact with data objects containing multiple data types. This lets you, for example, interact with complex data structures from WebGL, data files, or C structures you need to use while using js-ctypes.
Consider this C structure:
struct someStruct { unsigned long id; char username[16]; float amountDue; };
You can access a buffer containing data in this format like this:
var buffer = new ArrayBuffer(24); // ... read the data into the buffer ... var idView = new Uint32Array(buffer, 0, 1); var usernameView = new Uint8Array(buffer, 4, 16); var amountDueView = new Float32Array(buffer, 20, 1);
Then you can access, for example, the amount due with
amountDueView[0]
.Note: The data structure alignment in a C structure is platform-dependent. Take precautions and considerations for these padding differences.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With