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

perf(useQueries): optimize findMatchingObservers fn in queriesObserver for large datasets #5247

Merged
merged 2 commits into from Apr 10, 2023
Merged

perf(useQueries): optimize findMatchingObservers fn in queriesObserver for large datasets #5247

merged 2 commits into from Apr 10, 2023

Conversation

AEmad01
Copy link
Contributor

@AEmad01 AEmad01 commented Apr 10, 2023

This optimizes the findMatchingObservers function in queriesObserver which becomes very slow on large data sets in useQueries.

Below are the results with latest react query without this change on a dataset of 5000 objects.

image

We can see it takes 1.3 seconds only inside the findMatchingObservers function, which leads to a very laggy UX.
The majority of this time is spent inside 2 functions in findMatchingObservers.

  • The first function .flatMap is O(n*m) as it runs Array.find on every single element in defaultedQueryOptions. By using a map we bring this down to O(m), where m is the number of elements in defaultedQueryOptions. Yes using a map will add more space complexity but the overhead is extremely minimal compared to the performance gain.
  • The second function is where we check for matchedQueryHashes which uses Array.includes and is again a O(n*m) function which can be brought down to O(m) by using a Set.

Measurements:
The following is setup with 5000 queries in useQueries with unique keys and everything matching documentation. When invalidating one query, the following happens:

  • Before the fix, it takes 1.28 seconds only inside findMatchingObservers
    image

  • After the fix, it only takes 47ms! Nearly a 30x performance improvement for a dataset of 5000 objects.
    image

If required & the very minimal changes are not justified, I can provide codesandbox for both comparisons.

@vercel
Copy link

vercel bot commented Apr 10, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
query ⬜️ Ignored (Inspect) Apr 10, 2023 3:10am

@AEmad01 AEmad01 changed the title perf(queriesObserver): optimize findMatchingObservers fn in queriesObserver for large datasets perf(useQueries): optimize findMatchingObservers fn in queriesObserver for large datasets Apr 10, 2023
@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 10, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 52c8fa9:

Sandbox Source
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

@nx-cloud
Copy link

nx-cloud bot commented Apr 10, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 52c8fa9. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 4 targets

Sent with 💌 from NxCloud.

@TkDodo TkDodo merged commit a317633 into TanStack:main Apr 10, 2023
7 checks passed
@AEmad01 AEmad01 deleted the fix-findMatchingObservers-perf branch April 10, 2023 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants