Skip to content

perf(Announce): skip getComputedStyle when there is no text content to announce - #7546

Merged
hectahertz merged 4 commits into
mainfrom
hectahertz/perf-announce-skip-empty-reflow
Feb 18, 2026
Merged

perf(Announce): skip getComputedStyle when there is no text content to announce#7546
hectahertz merged 4 commits into
mainfrom
hectahertz/perf-announce-skip-empty-reflow

Conversation

@hectahertz

@hectahertzhectahertz commented Feb 14, 2026

Copy link
Copy Markdown
Contributor

Closes #

Announce calls window.getComputedStyle(element) to check if the element is visible before announcing. This forces synchronous style recalculation for the entire DOM. When Announce renders with initially empty content (e.g. TreeView's AriaStatus live region, which starts with ariaLiveMessage = ''), this reflow is completely wasted.

The fix: check getTextContent(element) first. If empty, bail out before the expensive getComputedStyle call.

Benchmarks (TreeView StressTest, 1000 items, 14K DOM nodes)

MetricBeforeAfter
LCP2,398ms1,952ms
Render delay2,225ms1,806ms
Announce.tsx forced reflow294ms0ms

Changelog

New

N/A

Changed

  • Announce checks for empty text content before calling getComputedStyle, avoiding a forced synchronous style recalculation on mount when there's nothing to announce

Removed

N/A

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

  • All 71 live-region and TreeView tests pass
  • Updated one TreeView test that assumed live-region element would be created on initial empty-content mount. The element is now lazily created on the first real announcement instead.
  • To verify: open the StressTest story, run a Performance trace with reload, confirm no Announce.tsx forced reflow appears.

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Added/updated previews (Storybook)
  • Changes are SSR compatible
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge
  • (GitHub staff only) Integration tests pass at github/github

@changeset-bot

changeset-botBot commented Feb 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ac6635a

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

This PR includes changesets to release 1 package
NameType
@primer/reactPatch

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

@github-actions

Copy link
Copy Markdown
Contributor

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the integration-tests: skipped manually label to skip these checks.

@github-actionsgithub-actionsBot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Feb 14, 2026
@github-actions
github-actionsBottemporarily deployed to storybook-preview-7546 February 14, 2026 11:53 Inactive

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the Announce component's performance by reordering logic to check for empty text content before calling getComputedStyle, avoiding expensive forced style recalculations when there's nothing to announce. This is particularly beneficial for components like TreeView that render with initially empty live region content.

Changes:

  • Reordered announce logic to check text content before visibility styles, eliminating unnecessary getComputedStyle calls on empty content
  • Updated TreeView test to get live region after first announcement instead of before, reflecting the lazy creation behavior

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

FileDescription
packages/react/src/live-region/Announce.tsxReordered logic to check text content before getComputedStyle, avoiding forced reflows for empty announcements
packages/react/src/TreeView/TreeView.test.tsxUpdated test to get live region after first announcement, reflecting lazy element creation
.changeset/announce-skip-empty-reflow.mdAdded changeset documenting the performance improvement

@siddharthkpsiddharthkp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Surprised this makes that much of a difference!

@hectahertz

Copy link
Copy Markdown
ContributorAuthor

@siddharthkp I know, right?

@primer-integration

Copy link
Copy Markdown

👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/13894

@primer-integration

Copy link
Copy Markdown

Integration test results from github/github-ui:

Passed CI  Passed
Running VRT  Running
Passed Projects  Passed

@hectahertz
hectahertz added this pull request to the merge queueFeb 18, 2026
Merged via the queue into main with commit 86d6897Feb 18, 2026
54 of 55 checks passed
@hectahertz
hectahertz deleted the hectahertz/perf-announce-skip-empty-reflow branch February 18, 2026 17:44
This was referenced Feb 18, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-tests: recommendedThis change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@hectahertz@siddharthkp