2

I have a web app, and it has some variables in the main program. These variables are objects with lots of strings or arrays in them.

But then it makes 4 web workers. But it then sends the giant object as messages to each web worker. This basically clones the object 4 times.

I want to use the new sharedarraybuffer datatype http://lucasfcosta.com/2017/04/30/JavaScript-From-Workers-to-Shared-Memory.html to be able to have the web workers be able to access the object from the parent, so it can be more memory-efficient.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer

How can I do this? The example seems to initialize it by making it based on number of bytes needed. I don't understand how I would calculate that.

Does anyone know how to do this?

Thanks

omega
  • 40,311
  • 81
  • 251
  • 474
  • 1
    Hey, I just answered a similar question with more detail here: https://stackoverflow.com/a/57326943/607407 But as you probably know, SharedArrayBuffer is unavailable for security reasons. – Tomáš Zato Aug 02 '19 at 14:04

1 Answers1

1

You cannot really use that for the data structure you describe. I mean theoretically you could use getters and setters that would refer to byte offsets in the underlying shared byte array, but that would be really complicated project, most likely not worth the effort unless you're doing it for fun.

You will need to re-think your data structure if you want to use shared memory. You need to ensure that it's constant size and you need to simplify it.

Try to use profiler to see which part of your object is biggest and try start by only sharing that part.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778