CHAPTER 27
Beginner
Performance and Optimization
Updated: May 16, 2026
7 min read
# CHAPTER 27
Performance and Optimization
1. Introduction
React Native apps run on phones, which have significantly less processing power and RAM than desktop computers. If you write careless JavaScript, your app will drain the user's battery, drop frame rates during animations, and eventually crash. In React, the biggest performance killer is unnecessary re-renders—forcing the phone to redraw a component that hasn't actually changed. In this chapter, we will master Performance and Optimization. We will learn the advanced React triad:React.memo, useMemo, and useCallback, caching our UI and calculations to achieve a flawless 60 Frames Per Second (FPS).
2. Learning Objectives
By the end of this chapter, you will be able to:- Identify the cascading re-render problem in React.
-
Use
React.memoto cache pure UI components.
-
Use the
useMemohook to cache heavy mathematical calculations.
-
Use the
useCallbackhook to stabilize function references across renders.
-
Apply aggressive optimization strategies to
FlatList.
3. The Re-Render Problem
When a Parent component's state changes, React automatically re-renders the Parent AND every single Child component inside it. Imagine an App with a simple Counter, and a Child component displaying a static heavy image.
javascript
4. React.memo (Caching the UI)
We can tell React to skip redrawing the child component by wrapping it in React.memo.
memo says: *"Only redraw this component if its PROPS change. If the props are identical to the last render, use the cached visual image."*
javascript
5. useMemo (Caching Calculations)
If your component performs a heavy math calculation (like sorting an array of 10,000 products), it will perform that calculation *every single time the component re-renders*, even if the user is just typing in a search box.
We use useMemo to cache the *result* of the calculation!
javascript
6. useCallback (Caching Functions)
In JavaScript, const a = () => {} and const b = () => {} are NOT the same function in memory.
Every time a React component re-renders, it recreates all of its internal functions from scratch.
If you pass a recreated function down to a child component as a prop, the child says, *"Oh, this function prop is brand new! I must re-render!"* — completely destroying the React.memo optimization we did in Step 4.
We use useCallback to cache the *definition* of the function itself!
javascript
7. FlatList Optimization
TheFlatList is the most performance-sensitive component in React Native. If it lags, the app feels broken.
-
1.
Always use
keyExtractor: Prevents the list from rebuilding identical items.
-
2.
Optimize
renderItem: The function passed torenderItemMUST be wrapped in auseCallbackso it isn't recreated 100 times.
-
3.
Memoize List Items: The individual
<ProductCard>you return insiderenderItemMUST be wrapped inReact.memo.
javascript
8. Visual Learning: The Memoization Triad
txt
9. Common Mistakes
-
Memoizing Everything: A massive beginner mistake is wrapping literally every single variable and component in
useMemoandmemo. Memoization requires RAM to store the cache. If you cache thousands of tiny, fast components, the RAM usage will skyrocket, and checking the cache will actually take longer than just redrawing the UI. ONLY memoize highly complex UI trees or heavy array operations.
10. Best Practices
-
Console.log Auditing: The easiest way to find performance leaks is to put a
console.log("Drawing Component X")at the top of your component. If you click a button and the terminal spits out "Drawing Component X" 50 times, you have a massive re-render leak. Fix it with the triad above!
11. Practice Exercises
-
1.
What higher-order React function is used to wrap an entire UI component so that it only re-renders if its explicit
propschange?
-
2.
If you are passing an
onPressarrow function down into a heavily optimized<FlatList>child component, what hook must you wrap the arrow function in to preserve its memory address?
12. MCQs with Answers
Question 1
A component contains a useState hook managing a text input, and a heavy function that calculates prime numbers. The prime number calculation takes 3 seconds and freezes the UI every time the user types a single letter. Which hook should be used to cache the prime number result so it isn't recalculated on every keystroke?
Question 2
Why does passing an inline arrow function (e.g., onPress={() => doSomething()}) as a prop to a child component explicitly break the React.memo optimization on that child?
13. Interview Questions
-
Q: Explain the mechanical difference between
useMemoanduseCallback. What exactly is being cached in each scenario?
-
Q: Detail the cascading re-render behavior in React. How does
React.memointercept this behavior, and what is the computational cost of usingReact.memoeverywhere?
-
Q: A developer has implemented a
<FlatList>rendering 5,000 items. Scrolling is incredibly jittery. Detail three specific optimizations you would implement regardingrenderItem, list keys, and component memoization to achieve 60 FPS.
14. FAQs
Q: My app is slow in the Expo Go development environment. Does this mean it will be slow in production? A: No! The development environment runs a massive amount of debug code (Hot Reloading, Console logging, Error tracking) that slows the engine down significantly. Always test performance on a fully compiled Production build before panicking!15. Summary
In Chapter 27, we transitioned from writing functional code to writing enterprise-grade, highly optimized code. We identified the cascading re-render pipeline and surgically intercepted it using the React memoization triad. We utilizedReact.memo to cache heavy UI architectures, deployed useMemo to store the results of computationally expensive array mathematics, and applied useCallback to stabilize function references across render cycles. You now possess the skills to maintain a flawless 60 FPS under heavy data loads.