The React hook useMemo avoids unnecessary work by reusing the result of an expensive calculation when its dependencies have not changed. Used correctly, it helps keep components efficient. But when objects or arrays are created during render and included in dependency lists, that optimization breaks down. The impact is subtle at first and often goes unnoticed until it becomes a real performance problem.
How memoization breaks
React compares dependencies using strict equality. Any object created inside the component body receives a new reference each render. Even if the contents do not change, React sees the reference as different and recomputes.
Consider a task list component that filters and sorts tasks:
// Problem: filtersConfig is recreated on every render
function TaskList({ tasks, showCompleted }) {
const filtersConfig = {
includeCompleted: showCompleted,
sortKey: 'dueDate',
sortDirection: 'asc'
};
const visibleTasks = useMemo(() => {
return applyFiltersAndSort(tasks, filtersConfig);
}, [tasks, filtersConfig]); // Always treated as changed
return <TaskItems tasks={visibleTasks} />;
}Even when showCompleted does not change, filtersConfig receives a new reference on every render. React recomputes applyFiltersAndSort each time, removing the benefit of memoization.
It gets worse
At first, the filter logic is small and fast. As the feature grows, more fields get added… priority, categories, user-specific rules… Each addition makes the transformation more expensive. What used to take microseconds may now take much longer. Because it still runs on every render, the UI gradually slows down, and the cause is not immediately obvious.
The issue is not the complexity of the filters. The real cause is the unstable reference that has been present since the beginning.
Stable references
Dependencies must remain stable unless their value meaningfully changes. Let’s look at how to do that.
Use module level constants
For static options, declare them outside the component so they are created only once.
// Stable reference loaded once
const DEFAULT_FILTERS = {
sortKey: 'dueDate',
sortDirection: 'asc'
};
function TaskList({ tasks }) {
const visibleTasks = useMemo(() => {
return applyFiltersAndSort(tasks, DEFAULT_FILTERS);
}, [tasks, DEFAULT_FILTERS]);
return <TaskItems tasks={visibleTasks} />;
}Memoize dynamic objects
If the configuration depends on props, memoize it before passing it to other memoized logic.
function TaskList({ tasks, showCompleted }) {
const filtersConfig = useMemo(() => ({
includeCompleted: showCompleted,
sortKey: 'dueDate',
sortDirection: 'asc'
}), [showCompleted]); // Stable unless showCompleted changes
const visibleTasks = useMemo(() => {
return applyFiltersAndSort(tasks, filtersConfig);
}, [tasks, filtersConfig]);
return <TaskItems tasks={visibleTasks} />;
}
And that’s pretty much it.
By ensuring all objects and arrays in dependency lists are stable, you preserve the guarantees useMemo provides and avoid unnecessary work.
Commit to stable references and keep your memoization effective.
Leave a Reply