A React Native library that enables JavaScript code execution in a separate thread using the Hermes engine.
- Run JavaScript code in a background thread to avoid blocking the main UI thread
- Uses Hermes engine for JavaScript execution
- Supports both old and new React Native architectures
- Cross-platform support (iOS & Android)
- TypeScript support
npm install react-native-hermes-worker
# or
yarn add react-native-hermes-workerAdd the Hermes Worker babel plugin to your app's babel.config.js:
module.exports = {
plugins: [
'react-native-hermes-worker/plugin'
]
};This plugin transforms JavaScript functions into a format that can be executed in the worker thread.
import { startProcessingThread, stopProcessingThread, enqueueItem } from 'react-native-hermes-worker';
// Start the worker thread
startProcessingThread();
// Execute code in background
const result = await enqueueItem(() => {
// Heavy computation here
return "computation result";
});
// Stop the worker thread when done
stopProcessingThread();You can compile custom JavaScript code into Hermes bytecode to initialize the worker thread with predefined functions.
Create a JavaScript file with your worker code:
// worker/index.js
// Define functions available to the worker
function heavyComputation(data) {
// Your computation logic
return result;
}
// Expose functions to the worker scope
globalThis.heavyComputation = heavyComputation;Create a hermes-workers.json in your project root:
[
"./src/worker/index.js"
]Run the compile script from your project root:
npx compile-hermes-bundlesThis will compile your worker files into Hermes bytecode. The compiled files will be placed in:
- iOS:
ios/assets/index.worker.bundle.hbc - Android:
android/app/src/main/assets/index.worker.bundle.hbc
You need to manually add the .hbc file to your Xcode project:
- Open your project in Xcode
- Right-click on your project's target
- Select "Add Files to [YourProject]"
- Navigate to
ios/assets/ - Select
index.worker.bundle.hbc - Make sure "Copy items if needed" is checked
- Add to your main application target
No additional setup required. The asset will be automatically bundled.
// Initialize worker with custom bytecode
// Note: Only pass the base name of your entry file, without extensions
startProcessingThread('index'); // Will look for 'index.worker.bundle.hbc'
// Now you can call your predefined functions
const result = await enqueueItem('heavyComputation(data)');- iOS 15.1+
- Android API level 24+
- React Native 0.71.0+
- Supports both the old and new React Native architectures
- Uses TurboModules when available
- Falls back to the legacy native module system on older versions
# Install dependencies
yarn install
# Run example app
yarn example ios
# or
yarn example androidMIT License - see LICENSE file for details
See CONTRIBUTING.md for details on how to contribute to this project.