I've got a question about selector memoization with Reselect.
As far as I understand the documentation of Reselect, the following implementation is the proposed and correct way to memoize a selector that expects a parameter:
const selectOrdersByCustomer = createSelector(
[
state => state.orders,
(state, customerId) => customerId,
],
(orders, customerId) => {
return orders.filter(order => order.customerId === customerId);
}
);
const orders = useSelector(state => selectOrdersByCustomer(state, customerId));
One of my colleagues came up with this approach of the same selector:
const selectOrdersByCustomer = customerId => createSelector(
state => state.orders,
orders => {
return orders.filter(order => order.customerId === customerId);
}
);
const orders = useSelector(selectOrdersByCustomer(customerId));
(I simplified this, the actual implementation in our application is a fair bit more complicated)
I tried to add console.count('counter'); to the component where this selector is used and it seems like both implementations trigger the same amount of rerenders.
My question: Is there a performance penalty of the second implementation in comparison with the first one?
The second way has a performance issue. Each selector has its own cached result, but a new memoized selector function is created every time the component renders. It will run the output selector multiple times. The cached result will not be used.
Logs: