Search code examples
javascriptserializationdata-transfer

Transfer an 8 mb double array to javascript


I have a 1024x1024 double matrix (i.e. 8mb array) which I need to use in a javascript app.

I found that using sylvester is quite useful for manipulating my matrix. My problem right now is simply to find an efficient way to load this array into a web page.

It seems quite tedious loading binary data into javascript. I thought using images as a mean of transfer but it seems quite tedious too...

For the record, it is a numpy ndarray, so I can play around with the format fairly easily from Python.

Thanks.

Edit: Right now I am dumping my raw array as a string and parsing it with the Binary Parser by JSFromHell but it's really, quite, slow...


Solution

  • The best way would be the binary-capable XMLHttpRequest, see https://developer.mozilla.org/En/Using_XMLHttpRequest#Receiving_binary_data_using_JavaScript_typed_arrays. You could load the data directly into a Float64Array and it would be really fast. The downside: the spec wasn't finalized until recently (as you can see, Firefox 4/5 implements one approach while Firefox 6 will use another) and it is only implemented by Firefox and Chrome so far (with Chrome working the same as Firefox 6). So if performance is really important you could use this approach when possible (XMLHttpRequest has responseType or mozResponseArrayBuffer property) and fall back to the slow code otherwise. The hope would be that most people update their browsers soon and take the fast route.