I really love the idea of bringing fully-fledged Web Workers API to React Native - it's something we wanted to add in Worklets for a long time, but it's lot more complicated to do right than we anticipated initially. We keep finding more stuff we have to implement first, before we can do workers.
Due to that, I have to debunk some claims in the comparison.
1. 🔄 Worklets also allow you to spawn as many JS environments as you'd like - all of them have an event loop, timers etc. from the start. In Bundle Mode you can also import on them any library you'd want. You can even customize them further, for example react-native-audio-api(-worklets) creates a Worklet Runtime for processing without an event loop and a thread/queue, since they provide these themselves
github.com/software-mansion/…
2.❌Unfortunately that's not true. You can use *some* of the Native Modules without issues, but even these might now work as you'd expect. Native Modules in React Native are closely tied to the main React Native runtime (RN Runtime). For example, all events from the modules are sent to the RN Runtime. react-native-workers then forward these events to its own worker runtimes, copying the payloads etc.
It means that even if you sent a HTTP request from a worker, the response comes to the RN Runtime, which runs on the JS thread. If the JS thread is busy, then your worker is starved too.
Also Native Modules usually aren't thread safe - they often assume that they're always called from the RN Runtime and the JS thread.
This is why in Worklets we had to implement fetch from scratch - currently it's gated behind a feature flag
docs.swmansion.com/react-nat…. It runs truly off the JS thread and would never get bottlenecked.
3.✅Here I must agree. We know Worklets aren't beginner friendly and we continue to make the API more approachable and intuitive.
4.🔄I think it's a bit more complicated. If you send a worklet to be executed on a dedicated Worklets Runtime runtime it's effectively a Web Worker - just without the onMessage API etc.
5.✅That makes sense - Worklets are supposed to be an imperative, low-level building block to make all kind of solutions. Web Workers are a higher-level abstraction.
6.🔄Worklets allow you to do the threading as you'd like. You can create a Worklet Runtime and juggle it between threads as you'd like with the C++ API. This is what Reanimated has been doing for years with the UI Worklet Runtime.
7.✅Debuggability out-of-the box is impressive. We have been trying to add debugger support to Worklets for a long time, but it was very difficult pre-Bundle Mode - and also due to how fast the RN Dev Tools have changed recently. Now that the dust has settled we're definitely going to revise this issue.
Let me add a few more points to the comparison in favor of react-native-workers (so I wouldn't look too salty 😇)
8.💎Bundle size loaded onto the Workers.
With Bundle Mode we load the entire JS Bundle onto each extra runtime - this comes at little cost, since it's mmaped in production - but it uses a lot of memory in development. We want to explore some bundle splitting techniques to improve the DX here - Workers nailed it.
9.💎No-copy memory sharing.
Worklets are still missing good no-copy memory abstractions - we've been working to make ArrayBuffers transferable without copies. We came into some threading and GC issues with Hermes when designing the imperative API and decided to postpone the idea. I haven't looked deeply into the code of Workers, but if Ammar got that feature right, huge respect for him.
---
After giving it some thought, I think there's no need to compete here.
I think react-native-worklets 👷 and react-native-worklets 🧵could work integrate on a deeper level and both benefit. Worklets can provide battle-tested, low-level abstractions and Workers deliver a high-level, familiar end-user API 👷🤜🤛🧵
I asked Ammar to explain the difference between Workers and worklets.