JSI Host Objects: Exposing C++ to JavaScript
A JSI Host Object is a C++ object exposed directly to your JavaScript runtime, bypassing React Native's slow, serializing bridge. It's used in high-performance native modules like database adapters.
Why it exists
The original React Native bridge communicated between JavaScript and native code by serializing everything into JSON strings. This process is asynchronous and slow, creating a bottleneck for performance-sensitive tasks. JSI (JavaScript Interface) was created to allow direct, synchronous communication by giving C++ and JavaScript a shared understanding of objects.
The mental model
Think of a Host Object as a C++ object given a special passport to live inside the JavaScript world. It's a proxy or an ambassador. When your JavaScript code calls a method on this object, like myNativeDb.query(), the call is directly and synchronously forwarded to the underlying C++ object's method. There is no serialization, no message passing, and no queue.
How it works
To create a Host Object, you define a C++ class that inherits from jsi::HostObject. You must implement its virtual methods, primarily get() and set(). The get(name) method acts as a router: when JS code tries to access a property (e.g., myObject.someValue), this C++ method is invoked with the property name. Your code is then responsible for returning the appropriate jsi::Value, which could be a number, a string, or even a jsi::Function wrapping another C++ method. The C++ object's lifetime is managed manually.
When to use it
Use Host Objects when building performance-critical native modules where the latency of the asynchronous bridge is unacceptable. This is common for libraries that require high-throughput, synchronous communication. Prime examples include native database libraries like WatermelonDB or Realm, and sophisticated animation or gesture libraries that need to sync state between JS and native on every frame.
When not to use it
Avoid Host Objects for simple native modules where the functionality is not performance-critical and can be handled asynchronously. The complexity of writing and maintaining C++, especially with manual memory management, introduces significant risk. If your calls are infrequent and the data is simple, the traditional TurboModule or even the legacy bridge approach is safer and easier.
One canonical example
A native database library exposes its connection object to JavaScript as a Host Object. In JS, you can write const records = myDbConnection.query('SELECT * FROM users'). This query access triggers the get() method in C++, which identifies the property name "query", retrieves the C++ function for querying, and executes it immediately with the provided arguments, returning the results directly to JavaScript.
Interview question
What is the primary benefit of using a JSI Host Object in React Native?
- a.It enables direct, synchronous calls from JavaScript to C++ methods.Correct
- b.It provides automatic garbage collection for C++ objects within the JavaScript runtime.
- c.It simplifies native module development by abstracting away C++ complexities.
- d.It serializes data between JavaScript and native asynchronously for better performance.
Why? this is the answer
The card explicitly states JSI Host Objects allow "direct, synchronous communication" between C++ and JavaScript, bypassing the slow, serializing bridge. Distractors incorrectly suggest simplification of C++ development, asynchronous serialization, or automatic memory management, all of which contradict the card's details.
Just read this? Test yourself on what you have been reading.
Read the original → github.com
- #react-native
- #jsi
- #native-modules
- #c++
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react-native — each one lists the topics its interview covers.
See open roles