using selectorFamily in a loop is slow
See original GitHub issueHello I’m experiencing a weird performance issue regarding atomFamily and selectorFamily when used with big collections. Here is my code:
const resourcesByIdAtom = atomFamily({ key: "resourcesByIdAtom", default: initialState })
const resourceState = selectorFamily({
key: "resourceState",
get: (id) => ({ get }) => get(resourcesByIdAtom(id))
})
const resourcesState = selectorFamily({
key: "resourcesState",
get: ids => ({ get }) => {
return ids.map(id => get(resourceState(id)))
},
})
const useResourcesValue = ids => useRecoilValue(resourcesState(ids))
The idea is that we fetch and keep a lot of resources inside this atom resourcesByIdAtom
, and we are using atomFamily in order to be able to access them also directly, based on their id.
But, there is a case we need to pass a lot of ids directly and read the data for all of them.
So using useResourcesValue
with a big array of ids, it takes a lot of time to calculate, cpu is on 100% and the main thread is completely bloated.
Is there any suggestions? Am I doing something completely wrong?
I’d expect selectors, especially for something that haven’t change to be super fast.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:19 (7 by maintainers)
Top Results From Across the Web
selectorFamily(options)
The selectorFamily() utility returns a function which can be called with user-defined parameters and returns a selector. Each unique parameter value will return ......
Read more >Slow JavaScript function, how can I improve it?
You're finding all elements in the DOM matching a given selector, then asking whether an element you already have appears in that set....
Read more >Exploring Asynchronous Requests in Recoil
Discover the power of using asynchronous queries in Recoil. ... To make the query pre-fetchable, use a selectorFamily instead of a plain ...
Read more >Selection — BTB 0.4.0 documentation
In BTB, the selection problem is solved using the Selector family of classes. ... use later on to evaluate the performance of our...
Read more >Recoil.js — High-Performance State Management for ...
Recoil addresses this by adding the ability dynamic global state with the atomFamily and selectorFamily . The Context API also introduced ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After some digging I found that the
mergeDependencyMapIntoGraph
function will be called every time a get operation in the selector is called, and the size ofdeps
, which will be iterated, grows as the number ofget
function calls, hence as we call moreget
in the selector, the function becomes exponentially slower. Any thoughts on this? I assume this is here for a reason and is there any ways we can improve that? @drarmstrI’m probably not going to code it up, but a selector might be used to aggregate the total cost of items in an order. In that case you would have an atom of order, an atom family of products, and a selector for the totals. If the number of products ordered gets high that’s where the system breaks down. Not sure if this is a really realistic example though.
On Thu, May 6, 2021 at 4:57 PM Johnny Klironomos @.***> wrote:
– Thanks, Nate!