-2

I have an array on objects, meshes created with Three.js, that I want to do some operations in a web worker. So the questions is how do I post them to the worker?

From what I understand there's something called transferable objects that uses something called ArrayBuffer but I can't find any info about how to convert my array of objects to that. Or is this perhaps not possible?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
arpo
  • 1,831
  • 2
  • 27
  • 48
  • Read MDN articles on the topic, they are pretty clear. You seem to be unsure what even the transferable and ArrayBuffer is. MDN will clear your uncertainty. – Tomáš Zato Nov 11 '16 at 11:41

1 Answers1

1

Unless your object is already in binary buffer format, there is not performance benefit in converting it into that format and back. For some reason, this is something most Web Socket users fail to grasp - questions like this are asked all the time. My answer is always the same - if your concern is performance, do not convert anything:

Just use plain good ol':

worker.postMessage(myArray);

If you for some reason think I'm wrong about the performance, feel free to fact-check my claims using interactive snippet in this answer:

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Ok thanks! I'll convert my meshes to a JSON structure that regenerates the meshes objects in the worker. – arpo Nov 11 '16 at 13:46
  • @arpo Do not convert it. Let the browser handle it, it will be ok. Just put your array directly to `postMessage` and in the worker, you will receive it. – Tomáš Zato Nov 11 '16 at 18:42
  • 1
    That was the first thing I tried. It gives me this error: "Uncaught DOMException: Failed to execute 'postMessage' on 'Worker': An object could not be cloned." – arpo Nov 12 '16 at 12:14
  • @arpon ah, well. In that case, you will probably need some conversion. Structured clone algorithm can only convert Array and Object types. It cannot handle Function type for example. However if you use JSON, you risk loosing those special objects too - if you don't need them you can either use JSON as you planned or use some loop to remove them from the array. – Tomáš Zato Nov 12 '16 at 13:17
  • There should be a performance benefit if you decide to bypass Three.js and directly set the WebGL buffers. – Magnus Sep 19 '19 at 16:32