I have an array of objects which contains more objects as values. I'm working with Web Workers and I need to use this exact array of objects (or another object) in worker file. Problem is I couldn't find any solution to forward this array or object from main file to worker. I cannot use Shared Array Buffer because it needs Typed Arrays (or I couldn't find any solution). And stringifying doesn't work because after parsing the reserve array that keeping my objects which I am using to compare can not recognize object.
Asked
Active
Viewed 292 times
1 Answers
2
Two threads cannot share an array of objects. While it's possible to transfer arrays with some objects in them between threads (where the source thread loses access to those objects, until or unless the target thread sends them back), you can only do that with transferrable objects, and plain objects are not transferrable.
You'll need to use some kind of serialization turning the objects into strings or transferable objects, and then handle deserializing them.
And stringifying doesn't work because after parsing the reserve array that keeping my objects which I am using to compare can not recognize object.
You'll need to give them unique IDs that allow you to relate the serialized version back to the original.

T.J. Crowder
- 1,031,962
- 187
- 1,923
- 1,875