Frontend: When to Memoize (useMemo / useCallback / React.memo)
MediumTypeScript
Description
> This problem is paper-only - see explanation for the canonical React reasoning.
Premature memoization is one of the most common mistakes in React code reviews. The auto-grader validates the decision rules.
Given a function call shaped `{ computeCostMs, callsPerRender, isReferentialDep, sourceRowsLargerThan10k }`, return one of:
- `"useMemo"` if the value should be memoized (significant compute OR a referential dep, OR working over a large input).
- `"useCallback"` if it's a function passed as a prop AND the recipient is React.memo-wrapped (model: `isReferentialDep === true`).
- `"none"` otherwise.
**Decision tree implemented by the grader:**
1. computeCostMs > 5 → `"useMemo"`.
2. sourceRowsLargerThan10k → `"useMemo"`.
3. isReferentialDep → `"useCallback"`.
4. Otherwise → `"none"`.
(If both #1 and #3 hold, useMemo wins - the value itself needs caching.)
**For the auto-grader:** implement `memoizationAdvice(input)`.