Skip to content

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

Merged
hectahertz merged 4 commits intomainfrom
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 intomainfrom
hectahertz/perf-announce-skip-empty-reflow

Conversation

@hectahertz
Copy link
Copy Markdown
Contributor

@hectahertz hectahertz commented Feb 14, 2026

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)

Metric Before After
LCP 2,398ms 1,952ms
Render delay 2,225ms 1,806ms
Announce.tsx forced reflow 294ms 0ms

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
Copy link
Copy Markdown

changeset-bot bot commented Feb 14, 2026

🦋 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
Name Type
@primer/react Patch

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-actions github-actions bot 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-actions bot temporarily deployed to storybook-preview-7546 February 14, 2026 11:53 Inactive
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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.

File Description
packages/react/src/live-region/Announce.tsx Reordered logic to check text content before getComputedStyle, avoiding forced reflows for empty announcements
packages/react/src/TreeView/TreeView.test.tsx Updated test to get live region after first announcement, reflecting lazy element creation
.changeset/announce-skip-empty-reflow.md Added changeset documenting the performance improvement

Copy link
Copy Markdown
Member

@siddharthkp siddharthkp left a comment

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
Contributor Author

@siddharthkp I know, right?

@hectahertz hectahertz enabled auto-merge February 16, 2026 15:33
@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 queue Feb 18, 2026
Merged via the queue into main with commit 86d6897 Feb 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 free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-tests: recommended This 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