Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cacheSize option for defaultMemoize #210

Conversation

b6pzeusbc54tvhw5jgpyw8pwz2x6gs

As 3rd argument, now defaultMemoize() can get the cacheSize.

There are two arrays to store previous argument arrays, previous results.

let cacheArgsArr = []
let cacheREsultArr = []

cacheSize feature is designed as follows:

const createSelector = createSelectorCreator( defaultMemoize, void 0, 2 )
const powerSelector = createSelector([
      (state)=>state.num
], ( num )=> num*num )

console.log( powerSelector({ num: 1 }) )            // 1 (recomputation)
console.log( powerSelector.getCacheArgsArr() )      // [ [1] ]
console.log( powerSelector.getCacheResultArr() )    // [1]

console.log( powerSelector({ num: 2 }) )            // 4 (recomputation)
console.log( powerSelector.getCacheArgsArr() )      // [ [2],[1] ]
console.log( powerSelector.getCacheResultArr() )    // [ 4, 1 ]

console.log( powerSelector({ num: 3 }) )            // 9 (recomputation)
console.log( powerSelector.getCacheArgsArr() )      // [ [3],[2] ]
console.log( powerSelector.getCacheResultArr() )    // [ 9, 4 ]

console.log( powerSelector({ num: 2 }) )            // 4 (cache)
console.log( powerSelector.getCacheArgsArr() )      // [ [2],[3] ]
console.log( powerSelector.getCacheResultArr() )    // [ 4, 9 ]

In general, there is high a probability that last argument is used again.
So last arguemnts and result are added or updated to index: 0 in cacheArgsArrandcacheResultArr`

Please feel free to contact us if you have something lacking.

@codecov-io
Copy link

codecov-io commented Jan 10, 2017

Current coverage is 100% (diff: 100%)

Merging #210 into master will not change coverage

@@           master   #210   diff @@
====================================
  Files           1      1          
  Lines          12     18     +6   
  Methods         0      0          
  Messages        0      0          
  Branches        0      0          
====================================
+ Hits           12     18     +6   
  Misses          0      0          
  Partials        0      0          

Powered by Codecov. Last update 1143dcb...484b0a1

@coveralls
Copy link

coveralls commented Jan 10, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 484b0a1 on b6pzeusbc54tvhw5jgpyw8pwz2x6gs:master into 1143dcb on reactjs:master.

3 similar comments
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 484b0a1 on b6pzeusbc54tvhw5jgpyw8pwz2x6gs:master into 1143dcb on reactjs:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 484b0a1 on b6pzeusbc54tvhw5jgpyw8pwz2x6gs:master into 1143dcb on reactjs:master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 484b0a1 on b6pzeusbc54tvhw5jgpyw8pwz2x6gs:master into 1143dcb on reactjs:master.

@ivawzh
Copy link

ivawzh commented Apr 21, 2018

Thank @b6pzeusbc54tvhw5jgpyw8pwz2x6gs , this was an enlightenment.
What do you think about the already supported selector customization below:

import memoizer from 'fast-memoize'
import { createSelectorCreator } from 'reselect'

// Why not Lodash's memoize?
// Because that won't work when the function takes more than one argument. And this is fast.

export const createSelector = createSelectorCreator(memoizer)

fast-memoize allows unlimited cache size.

@adnelson
Copy link

adnelson commented Nov 6, 2019

@ivawzh that's a great solution -- it would be super helpful if this were in the README.md. It suggests to "Create a custom selector with a cache size greater than one." But doesn't provide any guidance on how to do so.

@markerikson
Copy link
Contributor

Superseded by #513 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants