reselect icon indicating copy to clipboard operation
reselect copied to clipboard

createSelectorCreator uses defaultMemoize function for resultFunc

Open meganwalker-ibm opened this issue 8 years ago • 1 comments

Here's my situation:

state: {
   "a": "foo",
   "b": {
      "c": "bar",
      "d": "baz"
     /* other properties inside `b` */
   }

(b is actually a far deeper series of nested objects (including b.c and every other property in b, but for simplicity sake I've left that out it here)

I have the following selector

createSelector(
   state => state.a,
   state => state.b.c,
   (a, c) => { /* some expensive computation here */ }
)

a changes rarely and is suitable for the default strict equality check. However b changes frequently (and the nature of the changes mean b.c fails the strict equality check), yet b.c itself rarely has a meaningful change. This causes the selector to be recomputed a lot needlessly.

So I attempted to use createSelectorCreator with a deep equal function, as per the documentation. My first attempt resulted in the following selector code:

const createDeepEqualSelector = createSelectorCreator(
  defaultMemoize,
  equal
)

const cSelector = createDeepEqualSelector(
  state => state.b.c
)

createSelector(
   state => state.a,
   cSelector,
   (a, c) => { /* some expensive computation here */ }
)

However this still called my main selector every time b was updated, even if b.c was unchanged.

Eventually I got it working by changing cSelector to be

const cSelector = createDeepEqualSelector(
  state => state.b.c,
  c => c
)

Having looked at the code it appears as though the problem is the use of defualtMemoize here rather than the user provided memoize and memoizeOptions.

I might be wrong about this being the cause, as I haven't yet managed to understand fully how this library is functioning internally, but it seem intuitiviely suboptimal that I need to add a dummy function for this usecase to work.

meganwalker-ibm avatar Mar 15 '18 13:03 meganwalker-ibm

I think this should be address in v4 (#351)

ellbee avatar Jun 22 '18 13:06 ellbee