Thuta Learning
ရှာဖွေရန်
AdvancedMobile Developmentintermediate

Performance Optimization

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Performance Optimization ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် code ရေးပြီး Expo Go ပေါ်မှာ run ကြည့်တတ်မယ်
  • Real app project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

React.memo(Component) က props မပြောင်းရင် component ကို re-render မလုပ်ဘဲ ကျော်ပေးပါတယ် — parent re-render ဖြစ်တိုင်း child component အားလုံး automatically re-render ဖြစ်တတ်တာကို ကာကွယ်ပေးပါတယ်။ useMemo(calculation, deps) က expensive calculation ရလဒ်ကို cache ထားပြီး, dependency မပြောင်းရင် ထပ်မတွက်ဘဲ cached value ပြန်ပေးပါတယ်။ useCallback(function, deps) က function reference ကို stable ထားပေးပါတယ် — function ကို prop အနေနဲ့ React.memo child ဆီ pass ချင်ရင် အသုံးဝင်ပါတယ် (function reference မတည်ငြိမ်ရင် React.memo က အလုပ်မဖြစ်တော့ပါ)။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

FlatList ရဲ့ renderItem function ကို component render တိုင်း inline function အသစ် ({item} => ...) ဖန်တီးနေရင် React.memo ကို child item component မှာ သုံးထားတောင် အလုပ်မဖြစ်ပါဘူး — useCallback ဖြင့် renderItem function ကို stable ထားရမှာပါ။ Optimization ကို premature မလုပ်ပါနှင့် — 'app hesitant ဖြစ်နေတယ်' ဆိုတဲ့ actual problem ရှိမှ profile ပြီး targeted optimization လုပ်ပါ။

Code နမူနာ

javascript
import { memo, useCallback, useMemo } from 'react';

const ProductCard = memo(function ProductCard({ product, onPress }) {
  console.log('Rendering:', product.name); // memo ရှိရင် unnecessary re-render တွေ မဖြစ်
  return <Text onPress={() => onPress(product.id)}>{product.name}</Text>;
});

function ProductList({ products }) {
  // Stable function reference — ProductCard memo ကို အလုပ်ဖြစ်စေဖို့
  const handlePress = useCallback((id) => {
    console.log('Pressed:', id);
  }, []);

  const sortedProducts = useMemo(
    () => [...products].sort((a, b) => a.price - b.price),
    [products],
  );

  return sortedProducts.map((p) => (
    <ProductCard key={p.id} product={p} onPress={handlePress} />
  ));
}
You should see
Console log ကနေ ProductCard component က products data ပြောင်းမှသာ re-render ဖြစ်တာ ('Rendering: ...' log နည်းလာတာ) တွေ့ရမည်။

၅ မိနစ် စမ်းကြည့်

Console.log ပါတဲ့ list component တစ်ခုကို React.memo မပါဘဲ/ပါ ၂ မျိုး run ပြီး re-render ကြိမ်ရေး ကွာခြားချက်ကို observe ကြည့်ပါ။

သတိလေးတစ်ချက်

Performance optimization ကို 'actual measured problem' မရှိဘဲ everywhere ထည့်ရင် code complexity တက်ပြီး bug risk တောင် တိုးနိုင်ပါတယ် — React DevTools Profiler ဖြင့် bottleneck ကို အရင်တွေ့မှ optimize ပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • React.memo/useMemo/useCallback ကို component/calculation တိုင်းမှာ default အနေနဲ့ ကြိုတင်ထည့်ခြင်း (premature optimization — code ရှုပ်ထွေးစေရုံပဲ)
  • useCallback ကို dependency array မှန်ကန်စွာ မထည့်ဘဲ stale closure bug ဖြစ်စေခြင်း

အခု ကိုယ်တိုင် စမ်းကြည့်

Console.log ပါတဲ့ list component တစ်ခုကို React.memo မပါဘဲ/ပါ ၂ မျိုး run ပြီး re-render ကြိမ်ရေး ကွာခြားချက်ကို observe ကြည့်ပါ။

You'll know it worked when: Console log ကနေ ProductCard component က products data ပြောင်းမှသာ re-render ဖြစ်တာ ('Rendering: ...' log နည်းလာတာ) တွေ့ရမည်။