In this lesson, you will learn how shared memory pointers allow JavaScript to manipulate native data structures without the overhead of data serialization.
The Bottleneck: Why Serialization is Slow
Up until now, you have learned about the "Bridge" (and its evolution into the Fabric/TurboModule architecture). Traditionally, when JavaScript needed to send data to C++ (or vice versa), it had to perform serialization. Imagine you have a complex list of data; JavaScript turns that list into a JSON string, sends it across the bridge, and C++ parses that string back into a native object. This process is slow, especially for high-frequency updates like animations or complex gesture tracking, because the data is effectively copied and converted twice.
Enter Shared Memory
"Cpp-to-JS Memory Sharing" solves this by allowing both JavaScript and C++ to point to the same physical location in your device's memory.
Instead of passing a copy of a value across the bridge, we pass a pointer (an address). Think of it like a shared document in the cloud: instead of emailing copies of a file back and forth, you and a colleague both look at the same link. If you change a number in that document, your colleague sees the update instantly.
How it looks in practice
While you won't write raw C++ memory management code every day, the concept works like this:
// Imagine a C++ object created in memory
auto mySharedData = std::make_shared<MyDataStructure>();
// We expose this pointer to JavaScript through the JSI (JavaScript Interface)
runtime.global().setProperty(runtime, "sharedData",
// We create a "HostObject" that JS can touch directly
createHostObject(runtime, mySharedData)
);
Once this is set up, JavaScript can access sharedData directly. When you update a property on that object in JS, it modifies the memory that the C++ code is also watching. Because there is no JSON serialization, there is zero "translation time." The data is accessed at native speeds, making your app feel incredibly responsive.
Key Takeaway: By using memory pointers instead of serializing data into strings, React Native eliminates the bottleneck of the bridge, allowing JavaScript and C++ to interact with the same data in real-time.