perf: skip redundant render-root and title writes on no-op ticks#363
Merged
Conversation
The shared dateObserver ticks every registered <relative-time> on a single timer, calling update() on each. update() unconditionally rebuilt the [part=root] span via replaceChildren and re-set the title attribute even when the computed text/title were identical to what was already rendered (common on periodic ticks, e.g. an item that still reads "3mo").
Guard #updateRenderRootContent to reuse the existing span when the content and aria-hidden state are unchanged, and skip setAttribute('title') when the title is unchanged. This avoids needless DOM churn and style invalidation on every no-op tick.
Reuse the existing part="root" span across ticks and update textContent/aria-hidden in place, skipping the write when unchanged. Avoids node allocation + replaceChildren on every change and keeps a stable part node. Also types #renderRoot as Node & ParentNode to drop the Element cast.
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes periodic <relative-time> updates to reduce redundant DOM writes.
Changes:
- Reuses the render-root span and conditionally updates its content.
- Skips unchanged
titleassignments. - Adds render-root reuse tests.
Show a summary per file
| File | Description |
|---|---|
src/relative-time-element.ts |
Implements no-op write optimizations. |
test/relative-time.js |
Tests render-root node reuse. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 4
- Review effort level: Medium
Comment on lines
+121
to
+125
| // A subsequent update that produces the same text must not replace the node. | ||
| el.update() | ||
| await Promise.resolve() | ||
| const rootAfter = el.shadowRoot.querySelector('[part="root"]') | ||
| assert.equal(rootAfter, root, 'render root node should be reused when text is unchanged') |
| if (!this.#customTitle) { | ||
| newTitle = this.#getFormattedTitle(date) || '' | ||
| if (newTitle && !this.noTitle) this.setAttribute('title', newTitle) | ||
| if (newTitle && !this.noTitle && newTitle !== oldTitle) this.setAttribute('title', newTitle) |
| el.setAttribute('datetime', new Date(Date.now() - 3 * 60 * 60 * 1000).toISOString()) | ||
| await Promise.resolve() | ||
| const rootAfter = el.shadowRoot.querySelector('[part="root"]') | ||
| assert.equal(rootAfter, root, 'render root node should be reused across a text change') |
francinelucca
approved these changes
Jul 15, 2026
- Skip same-value aria-hidden setAttribute/removeAttribute on the render span - Add MutationObserver tests asserting no shadow-root writes on no-op ticks (including aria-hidden), and that an unchanged title is not rewritten
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
The shared
dateObserversingleton drives every registered<relative-time>on a single timer, callingupdate()on each element per tick. Todayupdate():[part="root"]span viareplaceChildren(span)in#updateRenderRootContent, andtitleattribute,even when the computed text/title are identical to what is already rendered. This is common on periodic ticks (e.g. an item that still reads
"3mo", or any element whose display hasn't crossed a unit boundary since the last tick). Each no-op write still invalidates style for the element, so pages with many<relative-time>elements pay avoidable style-invalidation churn every tick.This is the "skip the DOM write when nothing changed" optimization — no public API change, no behavior change.
Changes
#updateRenderRootContentnow reuses the existing[part="root"]span and mutates it in place rather than rebuilding it. The span is only recreated when it is missing or malformed (nopart="root"span, or extra children).textContentandaria-hiddenare each written only when their value actually changes, so unchanged ticks perform no DOM writes whilearia-hiddentoggles still take effect.update()skipssetAttribute('title', …)when the new title equals the current one.Tests
Added tests to
test/relative-time.js:update()produces the same text.update()emits no shadow-root mutations (verified with aMutationObserver), including anaria-hidden="true"case.titleis not written again on a no-opupdate().Full suite passes in Chromium.
Notes / scope
This is the low-risk, in-library slice of a broader perf discussion. It intentionally does not add offscreen pausing (an
IntersectionObserverindateObserver) — that's a larger, behavior-changing follow-up worth profiling first, sincecontent-visibility: autoin consuming apps already skips layout/paint for offscreen ticks.