Skip to content

perf(ui): build DOM nodes 3–4x faster - #149

Merged
natemoo-re merged 5 commits into
mainfrom
natemoo-re/ui-node-weight-a54c4d
Dec 3, 2026
Merged

natemoo-re merged 5 commits into
mainfrom
natemoo-re/ui-node-weight-a54c4d

Conversation

@natemoo-re

@natemoo-re natemoo-re commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

This PR improves the performance of Node creation after profiling. Eagerly creating Node properties before anything reads them was causing a lot of overhead. We now generate attribute properties the first time attributes is read, among some other laziness tweaks. (Lazy EventTarget setup was dropped from this PR, since #111 replaced that path with its own listener registry.)

No conflicts with #143 ❤️

Slop Output

This PR makes building DOM nodes with createElement, setAttribute, and appendChild 3–4x faster. Observable DOM behavior is unchanged.

A CPU profile of building small trees on main showed most of the time going to bookkeeping that nothing reads:

  • NamedNodeMap#changed (13.5%) rebuilt every indexed and named property on each attribute change, even when nothing had read element.attributes.
  • asciiLowercase and asciiUppercase (about 20%) ran a regex replace on every tag and attribute name.
  • Eight per-instance constant fields (about 6%) were copied onto every node.

Each commit fixes one of these and was measured on its own. The figures below compare each commit with the one before it, so the commits can be reviewed one at a time. They were measured before the rebase onto #111 and #150:

commit change
put node type constants on the prototype −7% to −10%
reflect attribute properties only once the map is exposed −31% to −51%
case-map ascii-only names natively −22% to −44%
push appended children instead of splicing −4% to −9%

Overall

Median µs per tree, from an in-process harness that alternates main (966ab37, after #111 and #150) and this branch. It was run twice, with the same ratios each time.

case main branch change
select (9 elements) 38.2 13.1 −66%
form (31 elements) 238.3 35.1 −85%
table (204 elements) 883.2 213.6 −76%
100 components (100 elements) 1112.9 263.8 −76%

Behavior

  • Attribute properties: a NamedNodeMap creates its indexed and named properties the first time element.attributes is read, then keeps them live. Script can only see these properties through attributes, so nothing observable changes.
  • Constants: the node type constants are on Node.prototype, as in browsers. Object.keys(node) no longer lists them.

Tried and dropped

These were measured and either didn't help or were too small to justify the change:

  • Creating NamedNodeMap lazily
  • Creating childNodes lazily. This was slower, and layout reads every node's children anyway.
  • A capped nodeName cache
  • Creating StyleDeclaration's !important set lazily
  • Sharing one HTML namespace object across createElement calls

@pkg-pr-new

pkg-pr-new Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

commit: fe7d37e

@natemoo-re
natemoo-re marked this pull request as ready for review December 2, 2026 03:16
@codspeed

codspeed Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 240 untouched benchmarks
⏩ 60 skipped benchmarks1


Comparing natemoo-re/ui-node-weight-a54c4d (fe7d37e) with main (966ab37)

Open in CodSpeed

Footnotes

  1. 60 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@cowboyd cowboyd 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.

It makes me giddy to think how performant this is going to be

Comment thread packages/ui/src/ascii.ts Resolved
Comment thread packages/ui/src/dom.ts Resolved
Comment thread packages/ui/src/named-node-map.ts Resolved

@dreyfus92 dreyfus92 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.

nice, the per commit numbers make this really easy to follow. overall looks good to me

Comment thread packages/ui/src/dom.ts Resolved
Comment thread packages/ui/src/ascii.ts Resolved
@natemoo-re
natemoo-re force-pushed the natemoo-re/ui-node-weight-a54c4d branch from c602acb to fe7d37e Compare December 3, 2026 03:26
@natemoo-re
natemoo-re merged commit c35d878 into main Dec 3, 2026
10 checks passed
@natemoo-re
natemoo-re deleted the natemoo-re/ui-node-weight-a54c4d branch December 3, 2026 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants