Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayBuffer vs Blob and XHR2

XHR2 differences states

The ability to transfer ArrayBuffer, Blob, File and FormData objects.

  • What are the differences between ArrayBuffer and Blob ?
  • Why should I care about being able to send them over XHR2 ? (I can understand value of File and FormData)
like image 360
Raynos Avatar asked Oct 15 '11 13:10

Raynos


People also ask

What is difference between ArrayBuffer and Blob?

An ArrayBuffer is in the memory, available for manipulation. A Blob can be on disk, in cache memory, and other places not readily available.

What is Blob ArrayBuffer?

arrayBuffer() The arrayBuffer() method in the Blob interface returns a Promise that resolves with the contents of the blob as binary data contained in an ArrayBuffer .

What is an ArrayBuffer?

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. It is an array of bytes, often referred to in other languages as a "byte array".

What is ArrayBuffer response type?

"arraybuffer" The response is a JavaScript ArrayBuffer containing binary data. "blob" The response is a Blob object containing the binary data.


1 Answers

This is an effort to replace the old method which would take a "string" and cut sections of it out.

You would use an ArrayBuffer when you need a typed array because you intend to work with the data, and a blob when you just need the data of the file.

Blobs (according to spec anyway) have space for a MIME and easier to put into the HTML5 file API than other formats (it's more native to it).

The ArrayBuffer lets us work with typed arrays which is much faster than string manipulation to work with specific bytes and lets us define what type the array segments actually are. Since JavaScript is not strictly typed, it's hard to take a file that might be broken into an array of 32bit ints or perhaps 64bit floats (just imagine 8 bit ints-- that'd be a nightmare in terms of performance with string manipulation and bitwise calculations, especially with unicode).

As far as I can tell you can always move a blob to an array buffer or to a string representation, but this being native to XHR allows scripts to be faster which is the main advantage.

I'd use a blob for working with the file API, but I'd use the array for preforming computation on the data.

like image 126
Incognito Avatar answered Nov 11 '22 14:11

Incognito