tezvyn:

Why use a C++ TurboModule, and what are the risks?

AI-drafted, machine-checkedSource: interviewadvanced
WHAT IT TESTS

low-level native integration.

OUTLINE

C++ JSI bindings give synchronous, serialization-free calls and shared cross-platform code; challenges are manual memory ownership across the JS-C++ boundary, GC interaction, and thread safety.

WHAT THIS TESTS: Whether you understand why JSI-backed C++ modules are fastest for hot paths and the real difficulties of bridging two memory models.

A GOOD ANSWER COVERS: A C++ TurboModule exposes host functions and host objects directly to the JavaScript engine through JSI, so JS invokes native code synchronously without serializing arguments to JSON and without an async queue, which is exactly what frequent, low-latency exchange needs. Writing the core in C++ also lets you share one implementation across iOS and Android. The challenges center on memory and concurrency. Ownership must be explicit: native objects referenced from JS must outlive those references, yet be released when JS drops them, which requires careful use of smart pointers and JSI's lifetime mechanisms rather than relying on the JS garbage collector, which does not free native heap memory. You must also respect that the JS runtime is single-threaded and not thread-safe, so values touched on the JS thread cannot be freely mutated from background threads.

COMMON WRONG ANSWERS: Assuming the JS GC collects native C++ allocations. Ignoring thread affinity of the JS runtime and calling into it from arbitrary threads. Holding raw pointers across the boundary and leaking or double-freeing. Believing synchronous always beats async even for heavy work that would block the JS thread.

LIKELY FOLLOW-UPS: Host objects versus host functions, how JSI runtime lifetime constrains references, when to dispatch heavy work to a background thread, and how CodeGen helps type safety.

ONE CONCRETE EXAMPLE: A real-time sensor module pushes readings many times per second. A C++ TurboModule exposes a host object whose getters return the latest values synchronously over JSI, with shared smart-pointer ownership ensuring the buffer lives as long as JS holds the object and is released on disposal, while a background acquisition thread synchronizes carefully so it never mutates state being read on the JS thread.

Read the original → callstack.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.