Resource sharing using N-API?

43 Views Asked by At

I was wondering if it was possible to use the N-API to give a workerthread access to an object that a different workerthread has created. Although I do not require concurrent access, I currently transfer data by serializing and deserializing it. However, my data is quite big (JSON.stringify().length() returns around 50000000), so I lose quite a bit of time. I know shared byte arrays exist, but serializing my object into those takes the same amount of time. Would N-API give me the possibility to give thread2 access to an object that thread1 has created with low overhead?

1

There are 1 best solutions below

0
On

This is possible only if your object can be accessed without V8 - ie it does not contain V8 objects. Binary buffers, TypedArrays work. JS Arrays, JS objects do not. Every worker_thread runs in a different V8 isolate and the V8 object storage is not thread safe and cannot be accessed from outside its main thread.