-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
perf: improve performance of util function "difference" in quriesObserver.ts in query-core package #9201
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
base: main
Are you sure you want to change the base?
perf: improve performance of util function "difference" in quriesObserver.ts in query-core package #9201
Conversation
View your CI Pipeline Execution ↗ for commit ab1b07f.
☁️ Nx Cloud last updated this comment at |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #9201 +/- ##
===========================================
+ Coverage 45.19% 59.56% +14.36%
===========================================
Files 206 138 -68
Lines 8240 5485 -2755
Branches 1861 1468 -393
===========================================
- Hits 3724 3267 -457
+ Misses 4076 1921 -2155
+ Partials 440 297 -143 🚀 New features to boost your workflow:
|
@@ -10,7 +10,7 @@ import type { | |||
import type { QueryClient } from './queryClient' | |||
|
|||
function difference<T>(array1: Array<T>, array2: Array<T>): Array<T> { | |||
return array1.filter((x) => !array2.includes(x)) | |||
return array1.filter((x) => !new Set(array2).has(x)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move the new Set
creation out of the filter
function to only create the Set once ?
I changed the internal behavior of the "difference" function in quriesObserver.ts from includes function to using has function in sets
And this is faster when the arrays are longer. I included a link to the code sandbox below so you can see the specific performance differences.
Codesandbox Link
https://codesandbox.io/p/devbox/crazy-cherry-627tsr