JavaScript: web worker and typed arrays

I have a web worker (started with new Worker() ) that does some processing and should return a Float32Array .
However, it seems that after working postMessage() data, it goes through serialization and deserialization in JSON, and as a result I get a message, this is a simple javascript Array (with all the properties that the original typed array had)

The trivial job should be to simply restore the typed array from the javascript array, but this is wasteful and takes time and memory.

Is there a better way to do this? Some way to describe JSON deserialization to create a Float32Array instead of a javascript array? or a way to transfer binary data in another way?

+4
source share
2 answers

All browsers that support working (except IE10) support what are called passed objects, which means that if you have an array buffer (i.e. take your .buffer property of your typed array), you can use postMessage as the second parameter include a list of array buffers that you want to transfer ownership back. This is much faster than copying.

+2
source

update: this is currently a Chrome bug:

http://code.google.com/p/chromium/issues/detail?id=73313

a typed array is saved in Firefox 4.

+1
source

Source: https://habr.com/ru/post/1347763/


All Articles