1

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.

1 Answer 1

2

Two threads cannot share an array of objects. As you mentioned, threads can share SharedArrayBuffer data, where the same data is accessed via typed arrays in both threads, but that's for primitive values, not for objects. (You could serialize an object to an array buffer, and deserialize it at the other end, perhaps only updating them object when the buffer data changes.)

It's possible to transfer other kinds of arrays with some types of 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 transferable objects, and plain objects are not transferable.

So you'll need to use some kind of serialization, turning the objects into strings or transferable objects or into byte arrays that you can share.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.