joltup / react-native-threads

Create new JS processes for CPU intensive work
MIT License
756 stars 142 forks source link

Worker thread has much slower performance #136

Closed mfbx9da4 closed 2 years ago

mfbx9da4 commented 2 years ago

I'm using this library to do some JS blocking file decryption on a worker process. This library works great to free up the main thread but the actual decryption time seems to be about 10x slower than doing the same decryption on the main JS process. Is the worker process somehow resource constrained? Why might this be?

Tested on iOS 15 in the simulator.

jacobp100 commented 2 years ago

You've got the cost to start up a new JS VM, parse the additional source code (+ parse the RN source code again), send the contents from your main JS process to the new one, and send it back.

Can you not use native code to do the decryption?

mfbx9da4 commented 2 years ago

Yep that's what I went with

mfbx9da4 commented 2 years ago

Is there a one off cost to constructing the new JS VM? ie is the worker re-used for subsequent calls?

jacobp100 commented 2 years ago

Yeah, the worker is created every time you do new Thread. Then any postMessage calls re-use it. It only gets removed if you explicitly call thread.terminate(). The cost is pretty huge to create the worker though (enough to justify Facebook building the Hermes JS engine)