JS Built-ins
redux-legacycreateMemoSelectoradvanced

Memoized Selector

πŸ“– Usage Example
var getVisibleTodos = createMemoSelector(function(state) {
  return state.todos.filter(function(t) { return !t.done })
})

getVisibleTodos(state) // computes
getVisibleTodos(state) // returns cached result

Memoised selectors prevent expensive recomputation when the state reference has not changed, improving render performance.

Memoized Selector

A memoized selector caches its last result and skips recomputation when the input hasn't changed:

function createMemoSelector(selector) {
  var lastInput, lastResult, called = false
  return function(state) {
    if (called && state === lastInput) return lastResult
    // ... compute and cache
  }
}

This is the core idea behind Reselect β€” expensive derived data is only recalculated when relevant state changes.

Challenge: Implement createMemoSelector(fn) that wraps a selector function with last-result caching based on input reference equality (===).

πŸ’‘ Hints (2)
  • β€’ Store the previous input with `var lastInput` and compare with `===` (reference equality).
  • β€’ The first call must always compute β€” use a `called` flag to handle the initial invocation.