Repository navigation
perf: batch elements into shared IntersectionObserver instances per threshold - #17
Closed
andreas-fahrecker wants to merge 1 commit into
Closed
andreas-fahrecker wants to merge 1 commit into
andreas-fahrecker wants to merge 1 commit into
Conversation
Owner
|
Thank your for the contribution!
If you ever run into a measurable performance problem here, I'd be happy to look at it again. Thanks again for taking the time. |
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.
Summary
Groups observed elements by their resolved threshold value and shares
one
IntersectionObserverper threshold, instead of creating a newIntersectionObserverfor every observed element.Motivation
observe()currently creates oneIntersectionObserverper element.On a page with many
intersect:elements sharing the same threshold(the common case, since most elements just use the default threshold
or
intersect-half/intersect-full), this means far more observerinstances than necessary. Since
restart()disconnects and rebuildsall observers from scratch, this cost is also repeated on every call
to it (e.g. on SPA route changes).
Changes
observe()now groups elements by their resolved threshold valuefirst, then creates a single
IntersectionObserverper distinctthreshold, observing all elements that share it.
entry.targetinstead of closing over the per-elementelementvariable, since a single callback now handles multiple elements.
intersect-onceelements are removed from their observer withobserver.unobserve(el)instead ofobserver.disconnect(), sincedisconnecting would now stop observing every other element sharing
that observer.
Backwards compatibility
No API changes. Behavior of
intersect-once,intersect-half,intersect-full, andrestart()is unchanged from the outside —this is an internal implementation change only.
Testing
Tested locally in an Astro project (not the repo's demo page), with
both this change and the arbitrary-threshold PR applied together:
intersect-oncestill stops observing the correct element oncetriggered, without affecting other elements sharing the same
observer/threshold.
intersect-oncecontinue to toggle in and out asexpected on repeated scroll in/out.
intersect-halfandintersect-fullstill resolve andtrigger correctly.
intersect-[value]thresholds (from the other PR) alsostill trigger correctly when combined with this change.