Skip to content

perf: read class attribute directly in selector adapter - #339

Open
PeronGH wants to merge 1 commit into
WebReflection:mainfrom
PeronGH:perf/class-attribute-selector
Open

PeronGH wants to merge 1 commit into
WebReflection:mainfrom
PeronGH:perf/class-attribute-selector

Conversation

@PeronGH

@PeronGH PeronGH commented Sep 28, 2026 •

Copy link
Copy Markdown

When matching selectors, getAttributeValue read class through element.classList.value, which creates the classList for every element it checks. This change reads the class attribute node directly:

const getAttributeValue = (element, name) => name === 'class' ?
  (element.getAttributeNode('class')?.value ?? null) : element.getAttribute(name);

Behaviour change (fix)

[class=""] used to match elements that have no class attribute, because classList.value is '' for them. Browsers only match elements that have an empty class attribute. Now linkedom does too. I added a test to test/html/document.js. It covers HTML and SVG elements, removeAttribute, className and classList.

Benchmark

2000 elements, 50 class-heavy selector groups, querySelectorAll run 500 times (Node, 3 runs each):

time
before ~1045 ms
after ~143 ms

Avoids materializing classList for every class lookup during matching, and fixes [class=""] matching elements without a class attribute.
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.

1 participant