Hermes Engine & Yoga Layout
📖 Concept
Hermes is Meta's JavaScript engine optimized specifically for React Native. It replaced JavaScriptCore (JSC) as the default engine and provides significant improvements in startup time, memory usage, and runtime performance.
Hermes key features:
- Ahead-of-Time (AOT) bytecode compilation: JS is compiled to bytecode at BUILD time, not at runtime. The app ships bytecode instead of JS text files.
- Reduced startup time: No parsing/compiling JS at launch — bytecode loads directly. Typical improvement: 30-50% faster start.
- Lower memory footprint: Optimized garbage collector, smaller runtime, no JIT compiler (intentionally — JIT uses more memory).
- Incremental GC: Short pauses instead of long stop-the-world GC cycles. Critical for 60fps rendering.
- ES6+ support: Proxies, async/await, Intl, WeakRef, FinalizationRegistry — most modern JS features.
Hermes limitations to know:
- No JIT compilation — pure interpreter + AOT bytecode. Computation-heavy loops are slower than V8/JSC with JIT.
eval()is limited — dynamic code evaluation doesn't benefit from AOT.- Some Intl features are behind flags or limited.
- Debugging uses Chrome DevTools Protocol (different from Safari for JSC).
Yoga Layout Engine: Yoga is Meta's cross-platform layout engine that implements a subset of CSS Flexbox. Every React Native component's layout is computed by Yoga.
Yoga key behaviors:
- Default
flexDirectioniscolumn(notrowlike web CSS) - Default
alignItemsisstretch - All dimensions are unitless (density-independent pixels)
- Percentage values are relative to the parent's equivalent dimension
- Yoga runs on the Shadow thread (old arch) or any thread (new arch)
- Absolute positioning uses
position: 'absolute'withtop/left/right/bottom
Layout performance considerations:
- Deep view hierarchies slow down Yoga calculations — flatten where possible
- Frequent layout recalculations (dynamic content, keyboard) trigger Yoga repeatedly
onLayoutcallbacks fire on the JS thread after Yoga completes — can cause additional re-renders
💻 Code Example
1// === HERMES OPTIMIZATION TECHNIQUES ===23// 1. Check if Hermes is running4const isHermes = () => !!global.HermesInternal;5console.log('Using Hermes:', isHermes());67// 2. Hermes bytecode compilation8// In metro.config.js, Hermes automatically compiles your JS:9// source.js → compiled.hbc (Hermes Bytecode)10// The .hbc file is what ships in the APK/IPA1112// 3. Profile-Guided Optimization13// Hermes supports basic profile-guided compilation14// Record hot functions → optimize their bytecode15// This is configured in the build system, not in code1617// 4. Memory optimization with Hermes GC18// Hermes uses a moving GC — objects can be relocated in memory19// This means: DO NOT hold native pointers to JS objects20// Use the new architecture (JSI) for proper C++ ↔ JS references2122// === YOGA LAYOUT DEEP DIVE ===2324// 5. Flexbox differences from web CSS25import { View, StyleSheet } from 'react-native';2627const styles = StyleSheet.create({28 // RN default: flexDirection: 'column' (web default: 'row')29 container: {30 flex: 1,31 // These are RN-specific defaults different from web:32 // flexDirection: 'column' (web: 'row')33 // alignItems: 'stretch' (web: 'stretch' — same)34 // flexShrink: 0 (web: 1)35 },3637 // Absolute positioning — same concept, simpler API38 overlay: {39 position: 'absolute',40 top: 0, left: 0, right: 0, bottom: 0,41 // No z-index needed usually — later in source = higher42 },4344 // Percentage sizes45 halfWidth: {46 width: '50%', // 50% of parent's width47 height: '100%', // 100% of parent's height48 },49});5051// 6. Layout performance: FLAT vs NESTED52// ❌ Deep nesting — Yoga must calculate more nodes53const DeepNested = () => (54 <View style={styles.wrapper}>55 <View style={styles.outer}>56 <View style={styles.middle}>57 <View style={styles.inner}>58 <Text>Content</Text>59 </View>60 </View>61 </View>62 </View>63);6465// ✅ Flattened — fewer Yoga calculations66const Flattened = () => (67 <View style={styles.container}>68 <Text style={styles.content}>Content</Text>69 </View>70);7172// 7. Avoiding layout thrashing73// ❌ Reading layout then immediately causing re-layout74const BadPattern = () => {75 const [height, setHeight] = useState(0);7677 return (78 <View79 onLayout={(e) => {80 // This fires → sets state → re-render → new layout → repeat81 setHeight(e.nativeEvent.layout.height);82 }}83 style={{ minHeight: height + 50 }} // Depends on height — layout loop!84 >85 <Text>Content</Text>86 </View>87 );88};8990// ✅ Use onLayout for reading, not for creating dependency loops91const GoodPattern = () => {92 const [height, setHeight] = useState(0);9394 return (95 <>96 <View onLayout={(e) => setHeight(e.nativeEvent.layout.height)}>97 <Text>Measured Content</Text>98 </View>99 {/* Height used by DIFFERENT component — no loop */}100 <View style={{ height: height + 50 }}>101 <Text>Sized based on above</Text>102 </View>103 </>104 );105};
🏋️ Practice Exercise
Hermes & Yoga Exercises:
- Profile your app's startup time with and without Hermes (if you can test both engines)
- Use
global.HermesInternal?.getRuntimeProperties()to inspect Hermes runtime details - Create a deep view hierarchy (10+ levels) and measure layout time using the Performance monitor
- Flatten a complex nested layout and compare Yoga computation time
- Implement a responsive layout using Yoga's percentage and flex properties that adapts to different screen sizes
- Create a layout that triggers a layout loop (onLayout → setState → re-render) and then fix it
⚠️ Common Mistakes
Assuming Hermes has JIT like V8 — Hermes intentionally omits JIT to save memory and startup time; heavy math loops will be slower
Using eval() or dynamic code loading with Hermes — bytecode is compiled at build time, dynamic code can't benefit from AOT
Not knowing that RN's flexDirection defaults to 'column' — coming from web CSS where default is 'row' causes layout confusion
Creating deeply nested view hierarchies — each nested View adds a Yoga node that must be calculated; flatten when possible
Using onLayout to create circular layout dependencies — measuring → setting state → re-layout → measuring again = infinite loop
💼 Interview Questions
🎤 Mock Interview
Mock interview is powered by AI for Hermes Engine & Yoga Layout. Login to unlock this feature.