About
React Native Worklets is a library that runs JavaScript code on more than one thread in a React Native app.
By default, React Native executes all of your JavaScript on one thread, the JS thread, in one JavaScript runtime. Rendering, layout, touch handling, network callbacks and your business logic all wait in the same queue. A long task on the JS thread delays everything that comes after it, and the app stops responding until the task ends.
Worklets adds more JavaScript runtimes to your app:
- The UI Runtime runs on the UI (main) thread. Code on it can respond to every frame and every touch without waiting for the JS thread.
- Worker Runtimes run on their own background threads. You create them for work that must not block either the JS thread or the UI thread.
Each runtime has its own JavaScript heap, so runtimes do not share JavaScript objects. A worklet function is a function that the Worklets plugin prepares at build time, so that you can copy it to another runtime and run it there. To move data between runtimes, Worklets gives you Serializables, Shareables and Synchronizables.
The example below runs the same app in two ways: with all of its work on the JS thread, and with the work distributed across runtimes. Use the Enable Worklets switch to compare them:
Code
Colours connect each executing thread to its current code line. Recently executed lines fade over the next three ticks.
CPU
Put it simply, a thread (a core) executes code and might obtain memory resources (like JavaScript Runtimes) for the duration of the execution.
Memory
JavaScript Runtimes and their Event Loops can be thought as exclusive memory resources. Event Loop is usually assigned to one thread and no other thread can obtain it. A Runtime can be obtained by other threads occasionally.
App output
The UI thread updates the app screen. Press the button to queue an input event and follow its result.