Skip to content

Instantly share code, notes, and snippets.

@maxired
Created February 10, 2025 14:44
Show Gist options
  • Select an option

  • Save maxired/ded0b53af78789e84d51115ca342f309 to your computer and use it in GitHub Desktop.

Select an option

Save maxired/ded0b53af78789e84d51115ca342f309 to your computer and use it in GitHub Desktop.
reselect deps check
import { createSelector, createSelectorCreator, weakMapMemoize } from 'reselect'
const firstArgsMemoize = (func) => {
const internal = weakMapMemoize(func)
return (state) => {
return internal(state)
}
}
const createStateOnlySelector = createSelectorCreator(
{
memoize: weakMapMemoize,
argsMemoize: firstArgsMemoize }
)
const aSelector = createSelector(state => state, state => state.a);
const bSelector = createSelector(state => state, state => state.b);
const addSelector = createStateOnlySelector([aSelector, bSelector], (a, b) => a + b)
const addSelectorWithProps =
createSelector(addSelector, (state, props) => props, (add, props) => add + props)
const state = { a: 3, b: 5}
console.log('add 3 + 5 ', addSelector(state, 1));
console.log('add 3 + 5 ', addSelector(state, 2));
console.log('add 3 + 5 ', addSelector(state, 3));
console.log('addSelectorWithProps 3 + 5 ', addSelectorWithProps(state, 3));
console.log('addSelectorWithProps 3 + 5 ', addSelectorWithProps(state, 10));
console.log('addSelectorWithProps 3 + 5 ', addSelectorWithProps(state, 18));
console.log('Selector recomputations', addSelector.recomputations());
console.log('Selector dependencyRecomputations', addSelector.dependencyRecomputations());
console.log('addSelectorWithProps recomputations', addSelectorWithProps.recomputations());
console.log('addSelectorWithProps dependencyRecomputations', addSelectorWithProps.dependencyRecomputations());
console.log('addSelectorWithProps 3 + 5 ', addSelectorWithProps({ a: 4, b: 6}, 18));
console.log('Selector recomputations', addSelector.recomputations());
console.log('Selector dependencyRecomputations', addSelector.dependencyRecomputations());
console.log('addSelectorWithProps recomputations', addSelectorWithProps.recomputations());
console.log('addSelectorWithProps dependencyRecomputations', addSelectorWithProps.dependencyRecomputations());
{
"name": "reselect-bench",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"reselect": "^5.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment