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

feat: store getComputedStyle result to avoid redoing that work #1048

Merged
merged 9 commits into from
Jul 24, 2024

Conversation

TomPridham
Copy link
Contributor

@TomPridham TomPridham commented Jun 5, 2024

does some work to address #1011
all this does is store the results of calling getComputedStyles from the isHidden function and reuse that later on when possible to avoid calculating it more than necessary. in the test it only saves a single extra call, but was 3x as fast in the test repo linked in that issue
use a caching version of getComputedStyle to avoid redoing that work since it's one of the more expensive functions called
updated timings with the new implementation
before:
Screenshot 2024-06-07 at 4 04 54 PM
after:
Screenshot 2024-06-07 at 4 02 59 PM

Copy link

changeset-bot bot commented Jun 5, 2024

🦋 Changeset detected

Latest commit: 109ccbd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
dom-accessibility-api Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines 361 to 364
// we don't cache the pseudoElement styles
if (pseudoElement !== undefined) {
return _getComputedStyle(el, pseudoElement);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could cache the pseudo element styles if the Map was of type MapLike<Element, {[key: pseudoElement]: CSSStyleDeclaration}>, but that seemed kind of over the top

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also didn't seem like it was possible they were called more than once per element

sources/accessible-name-and-description.ts Outdated Show resolved Hide resolved
sources/accessible-name-and-description.ts Outdated Show resolved Hide resolved
if (pseudoElement !== undefined) {
return _getComputedStyle(el, pseudoElement);
}
const cachedStyles = computedStyles.get(el);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth calling out that this lookup is not the expected O(1) but O(N) which is especially important since this may negate any benefit we get for caching.

I feel like we should only cache when the built-in Map is available. If you're testing in IE11, you don't need the caching anyway since you're in a browser already which has less issues with computing styles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes sense. i left a comment in the getComputedStyles function about the polyfill being slower. let me know if you want me to document that somewhere else as well

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying we shouldn't use the polyfill at all and only cache when we know the lookup is O(1).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i should have been clearer. i left a comment saying it was slower and that was why we were skipping using the caching if Map is undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just removed the polyfill completely so there won't be a chance someone stumbles into the slow behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did end up having to include the global type for Map because otherwise TS complained about needing to change the target

Copy link
Owner

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I added a changelog entry for review.

@TomPridham
Copy link
Contributor Author

sounds good. thanks!

@eps1lon eps1lon merged commit d9ad334 into eps1lon:main Jul 24, 2024
6 checks passed
@github-actions github-actions bot mentioned this pull request Jul 24, 2024
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