Skip to content

perf: keep the Mermaid SVG markup object stable - #693

Merged
farnabaz merged 1 commit into
vercel:mainfrom
bendrucker:mermaid-stable-svg-html
Oct 26, 2026
Merged

farnabaz merged 1 commit into
vercel:mainfrom
bendrucker:mermaid-stable-svg-html

Conversation

@bendrucker

@bendrucker bendrucker commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Mermaid passed a new { __html } object on every render. react-dom 19.2 rewrites innerHTML whenever that object changes identity, so each re-render reparsed the diagram's SVG and its <style> forced a document-wide style recalculation.

Diagrams re-render on every context change, which happens per token when options are passed inline, and once when a stream ends. Memoizing the object on the SVG string keeps the existing DOM.

On a Vite app with React 19.2.8, one stream of a mixed document on 2.7.0 cost:

Metric 2.7.0
SVG rewrites 357
SVG markup parsed 37.5 MB
ParseHTML 700–790 ms
UpdateLayoutTree ~1650 ms

Next.js 16.3's bundled React compares the markup first, so Next apps never paid this.

react-dom 19.2 assigns innerHTML whenever the dangerouslySetInnerHTML
object changes identity, without comparing the markup. Mermaid passed a
new { __html } object on every render, so each re-render of a finished
diagram reparsed its whole SVG and the SVG's <style> forced a document
style recalculation. Memoize the object on the SVG string.
@vercel

vercel Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@bendrucker is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

@farnabaz farnabaz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, Thanks

@farnabaz
farnabaz merged commit c6f5905 into vercel:main Oct 26, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants