Implement MutationObserver in the DOM polyfill - #624
Merged
Merged
Conversation
Assisted-By: devx/ca2f43ab-bbf9-4273-b1bc-0b06f8c90a57
Assisted-By: devx/ca2f43ab-bbf9-4273-b1bc-0b06f8c90a57
airhorns
force-pushed
the
polyfill-mutation-observer
branch
from
September 1, 2026 17:50
fb49553 to
7faf138
Compare
Contributor
|
Now that #652 has landed, I'd like to implement this as a built-in Window extension consumers can opt into |
andrewiggins
approved these changes
Sep 3, 2026
andrewiggins
left a comment
Contributor
There was a problem hiding this comment.
Gonna go ahead and merge this and then build on top of it
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
@remote-dom/polyfillcurrently installs and documents aMutationObserverconstructor, but the constructor is an empty stub:new MutationObserver(callback).observe(node)throws becauseobserveis undefined. This is especially easy to miss when remote code is type-checked againstlib.dom.This implements the observer surface over the polyfilled tree:
subtree,attributeFilter, and old-value optionsobserve(),disconnect(), andtakeRecords()Mutation delivery is integrated alongside the existing remote synchronization hooks, so observation does not change what is sent to a Remote DOM host.
Inactive-path performance
Mutation types maintain separate active-registration flags. Mutation sites check the corresponding flag before capturing old values or allocating a record,
Map, orNodeList, and before walking ancestors. The registrationWeakMapis created lazily and released again when the final observer disconnects. A child-list or character-data observer therefore does not add bookkeeping to attribute writes.In a local Node 20.20 benchmark of one million repeated
setAttribute()calls with no active observers (seven measured runs after warm-up), the median changed from 66.9ms to 47.0ms compared with the initial implementation in this PR, about 30% faster.Validation
pnpm exec vitest run(180 tests)pnpm lintpnpm type-checkmise exec node@20.20.0 -- pnpm --filter @remote-dom/polyfill build