-
Notifications
You must be signed in to change notification settings - Fork 471
feat(ByRole): Check only elements that could be a match #1046
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
Conversation
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 a592dba:
|
Codecov Report
@@ Coverage Diff @@
## main #1046 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 25 25
Lines 919 927 +8
Branches 283 288 +5
=========================================
+ Hits 919 927 +8
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
If I'm not mistaken here, the thing is that we have roles that are implicitly defined (by the elements) and we'll probably lose those if we filter using explicit role selector won't we? |
How did you verify that this improves performance? Please share the results so that we can judge whether this optimization is worth the added code. |
Handled here: dom-testing-library/src/queries/role.js Lines 190 to 192 in 77d3291
The matching on attributes with implicit roles as performed by the filter could not be handled by But narrowing the selection according to the element types which might be matched is possible. |
|
So this was only tested in JSDOM? Would be nice if you could share a fixture that we can run with this PR in any environment and post the findings you have. |
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.
Very helpful test fixture, thanks!
Something did set me off about this implementation. I think we tested this in the past and didn't see an significant gains.
Fiddled around with it a bit and it turns out that it can be slower for small DOM trees. Though for small DOM trees we're already fast enough that a slight slowdown (-~10%) is totally fine compared to having an order of magnitude faster queries for large DOM trees where perf really matters.
In the future, please include this work up front. If you already tested it locally, then sharing the work is in your interest as well for future reference. Perf improvements should just never go in untested.
🎉 This PR is included in version 8.9.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What:
Let
querySelectorAll
pre-filter elements if no normalizer is passed before running our own filters on them inqueryAllByRole
.Why:
byRole
is slow.How:
If
role
parameter is a string, we can predetermine which elements might match our filter functions.By dismissing other elements in
querySelectorAll
we reduce the time spent in our filter functions.Checklist:
docs site
Additional notes
I ran
getByAllRoles(container, 'table')
on the example document forrole-helpers
.The avg time spent dropped from
9.5ms
to4.2ms
.