Memoization

الحفظ في الذاكرة

Intermediateperformance1 min read
memoizeReact.memouseMemouseCallbackcache

Definition

Caching the result of an expensive computation so it only runs once with the same inputs — preventing unnecessary re-renders and recalculations in React.

تخزين نتيجة عملية حسابية مكلفة مؤقتاً حتى تعمل مرة واحدة فقط مع نفس المدخلات — مما يمنع إعادة التصيير والإعادة الحسابية غير الضرورية في React.

Why It Matters

The 404Fault admin glossary table renders 50 terms with status badges, action buttons, and bilingual content. Without memoization, every filter change re-renders all 50 rows. `React.memo` on the term row component eliminates these wasted renders.

يُصيَّر جدول قاموس المسؤول في 404Fault 50 مصطلحاً مع شارات الحالة وأزرار الإجراءات والمحتوى ثنائي اللغة. بدون الحفظ في الذاكرة، يُعيد كل تغيير في الفلتر تصيير جميع الصفوف الـ 50. `React.memo` على مكون صف المصطلح يُلغي هذه التصييرات المهدورة.

Full Definition

Memoization is an optimization technique that caches the return value of a function so it is only recomputed when its inputs change. In React: `useMemo` caches the result of an expensive calculation. `useCallback` caches a function reference so it doesn't change on every render. `React.memo` wraps a component to skip re-rendering if its props haven't changed. The key insight: React re-renders components on every parent re-render by default. If a child component is expensive to render and its props don't change, wrapping it in `React.memo` prevents wasted renders.
الحفظ في الذاكرة هو تقنية تحسين تُخزّن قيمة إعادة دالة مؤقتاً حتى لا تُحسَب إلا عندما تتغير مدخلاتها. في React: `useMemo` يُخزّن نتيجة حساب مكلف. `useCallback` يُخزّن مرجع دالة حتى لا يتغير مع كل تصيير. `React.memo` يُغلّف مكوناً لتخطي إعادة التصيير إذا لم تتغير خصائصه. الرؤية الأساسية: React يُعيد تصيير المكونات مع كل إعادة تصيير للمكون الأب بشكل افتراضي. إذا كان مكون فرعي مكلف التصيير ولم تتغير خصائصه، فإن تغليفه بـ `React.memo` يمنع التصييرات المهدورة.

Example Usage

Without memo: `function TermRow({ term }) { /* renders on every parent state change */ }`. With memo: `const TermRow = React.memo(function TermRow({ term }) { /* only re-renders if term prop changes */ });`. If filtering by status, only the filter state changes — the term objects themselves haven't changed, so memoized rows skip re-render.

بدون memo: `function TermRow({ term }) { /* يُصيَّر مع كل تغيير حالة للمكون الأب */ }`. مع memo: `const TermRow = React.memo(function TermRow({ term }) { /* يُعاد تصييره فقط إذا تغيرت خاصية term */ });`. عند التصفية بالحالة، تتغير فقط حالة الفلتر — كائنات المصطلح نفسها لم تتغير، لذا تتخطى الصفوف المحفوظة إعادة التصيير.

Knowledge Graph

Avoid these mistakes when using Memoization:

1

Memoizing everything — memoization has overhead; only use it when profiling shows a component is causing performance problems

2

Forgetting to memoize callback props passed to memoized components — if the parent creates a new function object on every render and passes it to a React.memo child, memo does nothing

3

Using useMemo for non-expensive calculations — `useMemo(() => a + b, [a, b])` is slower than just `a + b` due to the overhead of the memo hook itself

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using Memoization.

Explain:
1. What is Memoization and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Memoizing everything — memoization has overhead; only use it when profiling shows a component is causing performance problems, Forgetting to memoize callback props passed to memoized components — if the parent creates a new function object on every render and passes it to a React.memo child, memo does nothing, Using useMemo for non-expensive calculations — `useMemo(() => a + b, [a, b])` is slower than just `a + b` due to the overhead of the memo hook itself
5. Best practices and production tips

Official Resources