ArrayBuffer vs Blob and XHR2
Asked Answered
S

1

19

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)
Sermonize answered 15/10, 2011 at 13:31 Comment(0)
S
17

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.

Senator answered 15/10, 2011 at 14:43 Comment(1)
So basically for raw speed Blob is the way to go?Fuliginous

© 2022 - 2024 — McMap. All rights reserved.