question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using selectorFamily in a loop is slow

See original GitHub issue

Hello 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:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
tonyabracadabracommented, May 7, 2021

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 of deps , which will be iterated, grows as the number of get function calls, hence as we call more get 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? @drarmstr

2reactions
42shadow42commented, May 6, 2021

I’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:

No reason, since this is just a simplified example demonstrating the problem.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/facebookexperimental/Recoil/issues/914#issuecomment-833929099, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABUHIH7M5APKNOYGYFD3MODTMMNEZANCNFSM4YRIK2UA .

– Thanks, Nate!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found