I'm trying to use Web Workers to process large volumes of data, and when passing data back to the main thread for display, I would like to use a transferable object to reduce the impact on the UI thread.
The procedure currently results in a multi dimensional array that can also contain objects. For instance:
[{foo: [{bar: "Alice",
car: 23,
dab: [2, 3, 5]}],
faa: [{moo: {a: [2,3], b: [4,5]} },
{moo: {a: [6,7], b: [8,9]} }]},
{foo: [{bar: "John",
car: 33,
dab: [6, 7, 1]}],
faa: [{moo: {a: [5,5], b: [9,2]} },
{moo: {a: [7,7], b: [4,2]} }]},
...]
I have seen this string conversion post, but again, I can't see how to directly apply this to my array structure: Converting between strings and ArrayBuffers
Appreciate the help!
Lot's of people have problem understanding this. So let me give you a image of your options and what they do:
postMessage
with your data var object = { ... };
worker.postMessage(object);
object
to structured clone Note that creating and parsing structured clone is done by optimized native code.
var object = { ... };
var binary = CreateTypedArrayFromObject(object);
worker.postMessage(binary.buffer, [binary.buffer]);
object
to TypedArray
ArrayBuffer
of the TypedArray
to the [Worker] ArrayBuffer
What I'm pointing out is that you wanted to avoid copy, but you're still making a copy, only this time it's not native but javascript copy. If you want to optimize, you have to design your data structure so that it operates on typed arrays. If it doesn't, just don't even try to use them - you will just add extra overhead to your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.