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(synthetic-shadow): micro-optimize MutationObserver #3729

Merged
merged 1 commit into from
Sep 22, 2023

Conversation

nolanlawson
Copy link
Collaborator

Details

A small micro-optimization for our MutationObserver polyfill.

Also adds a benchmark, which is improved by 2%-6%:

Screenshot 2023-09-21 at 4 27 13 PM

I have other ideas for improvements, but I'm keeping this PR simple to make it easier to review.

Does this pull request introduce a breaking change?

  • ✅ No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • ✅ No, it does not introduce an observable change.

@nolanlawson nolanlawson requested a review from a team as a code owner September 22, 2023 15:55
characterData: true,
childList: true,
subtree: true,
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The most expensive MutationObserver is actually this one. It's very expensive because it gets triggered for mutations inside of a shadow root that it doesn't have access to, so the results have to be filtered out.

}
}
}
return result;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This diff is hard to read, but there are 2 small changes here:

  1. Convert ArrayReduce into a regular for-loop (this is the bulk of the improvement).
  2. Lazily access properties from the record rather than all at once (this is maybe a 1% perf improvement – apparently the browser does some work when you call these getters).

Copy link
Member

@ekashida ekashida left a comment

Choose a reason for hiding this comment

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

🍰 !

@nolanlawson nolanlawson merged commit 92de2c9 into master Sep 22, 2023
2 checks passed
@nolanlawson nolanlawson deleted the nolan/optimize-mutation-observer-ss branch September 22, 2023 18:44
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.

2 participants