Question

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)
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top