@miaskiewicz/turbo-dom 0.1.17 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,7 +14,7 @@ npm install -D @miaskiewicz/turbo-dom
14
14
 
15
15
  - ✅ **More compatible than happy-dom** — 99.72% on html5lib-tests vs happy-dom's 37%.
16
16
  Runs React Testing Library, `user-event`, downshift, Radix UI, and Headless UI unmodified.
17
- - ⚡ **Faster than jsdom** — ~19× lower per-file setup, 1139× faster HTML parsing.
17
+ - ⚡ **Faster than jsdom** — ~23× lower per-file setup, ~6× on query-heavy DOM work, 1837× faster HTML parsing.
18
18
  - 🎯 **Honest, not lying** — no fake layout numbers; `getBoundingClientRect()` is zeros and
19
19
  `getComputedStyle` reflects only what you set. Geometry tests belong in a real browser.
20
20
 
@@ -102,16 +102,19 @@ adopted yet.
102
102
 
103
103
  Measured on darwin-arm64, Node 24 (`npm run bench:all`):
104
104
 
105
- | benchmark | turbo-dom | happy-dom | jsdom |
106
- |---|---:|---:|---:|
107
- | per-file setup + 1 query (ops/s) | **6,808** | 526 | 266 |
108
- | full suite, 200 files (ms/file) | **0.13** | 1.45 | 3.36 |
109
- | parse 56 KB SSR (ops/s) | **502** | 46 | 23 |
110
- | parse 20 KB real page (ops/s) | **3,912** | 230 | 100 |
105
+ | benchmark | turbo-dom | happy-dom | jsdom | vs jsdom |
106
+ |---|---:|---:|---:|---:|
107
+ | per-file setup + 1 query (ops/s) | **5,950** | 611 | 260 | **22.9×** |
108
+ | full suite, 200 files (ms/file) | **0.13** | 1.50 | 3.38 | **23.6×** |
109
+ | query-heavy DOM work (iters/s) | **18,125** | | 3,089 | **5.9×** |
110
+ | parse 56 KB SSR (ops/s) | **478** | 43 | 26 | **18×** |
111
+ | parse 20 KB real page (ops/s) | **4,203** | 190 | 114 | **37×** |
111
112
 
112
113
  Why it's fast: parsing is native; the JS DOM doesn't allocate node objects for parts of the
113
- tree a test never reads; and `window` doesn't build the ~12 globals (storage, observers,
114
- matchMedia…) a render-only test never touches.
114
+ tree a test never reads; `window` doesn't build the ~12 globals (storage, observers,
115
+ matchMedia…) a render-only test never touches; and the selector/match engine is allocation-free
116
+ on the hot paths (no per-element `classList`/`split`/regex), so `querySelectorAll` and the
117
+ `getElementsBy*` collections that RTL leans on stay cheap.
115
118
 
116
119
  ## How it works
117
120
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miaskiewicz/turbo-dom",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "Faster, more spec-correct DOM for test runners — native html5ever (Rust/WASM) parser + lazy copy-on-write DOM. A drop-in-style alternative to jsdom/happy-dom for vitest & jest.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -64,10 +64,11 @@
64
64
  "bench:construct": "node bench/construct.mjs",
65
65
  "bench:suite": "node bench/suite.mjs",
66
66
  "bench:wasm": "node bench/wasm.mjs",
67
- "bench:all": "node bench/parse.mjs && node bench/construct.mjs && node bench/suite.mjs && node bench/wasm.mjs",
67
+ "bench:all": "node bench/parse.mjs && node bench/construct.mjs && node bench/suite.mjs && node bench/query.mjs && node bench/wasm.mjs",
68
68
  "prepublishOnly": "napi build --platform --release",
69
69
  "test:vitest": "vitest run -c test-vitest/vitest.config.mjs",
70
- "test:jest": "jest -c test-jest/jest.config.cjs"
70
+ "test:jest": "jest -c test-jest/jest.config.cjs",
71
+ "bench:query": "node bench/query.mjs"
71
72
  },
72
73
  "keywords": [
73
74
  "dom",
@@ -300,8 +300,8 @@ export class Element extends Node {
300
300
  get namespaceURI() { return nsUri(this.__ns); }
301
301
 
302
302
  // ---- attributes ----
303
- getAttribute(name) { const a = this.__attrs.find((x) => x.name === name); return a ? a.value : null; }
304
- hasAttribute(name) { return this.__attrs.some((x) => x.name === name); }
303
+ getAttribute(name) { const at = this.__attrs; for (let i = 0; i < at.length; i++) if (at[i].name === name) return at[i].value; return null; }
304
+ hasAttribute(name) { const at = this.__attrs; for (let i = 0; i < at.length; i++) if (at[i].name === name) return true; return false; }
305
305
  getAttributeNames() { return this.__attrs.map((a) => a.name); }
306
306
  setAttribute(name, value) {
307
307
  const a = this.__attrs.find((x) => x.name === name);
@@ -726,25 +726,43 @@ function toNode(doc, n) { return typeof n === 'string' ? doc.createTextNode(n) :
726
726
 
727
727
  function collectByTag(root, tag) {
728
728
  const out = [];
729
+ const all = tag === '*';
729
730
  const visit = (node) => {
730
- for (const c of node.__children()) {
731
- if (c.nodeType === ELEMENT_NODE) {
732
- if (tag === '*' || c.localName === tag) out.push(c);
733
- visit(c);
734
- }
731
+ const kids = node.__children();
732
+ for (let i = 0; i < kids.length; i++) {
733
+ const c = kids[i];
734
+ if (c.nodeType !== ELEMENT_NODE) continue;
735
+ if (all || c.localName === tag) out.push(c);
736
+ visit(c);
735
737
  }
736
738
  };
737
739
  visit(root);
738
740
  return out;
739
741
  }
742
+ // allocation-free whole-word class membership (no ClassList, no split)
743
+ function elHasClass(el, cls) {
744
+ const cn = el.getAttribute('class');
745
+ if (!cn) return false;
746
+ if (cn === cls) return true;
747
+ const L = cls.length;
748
+ let idx = cn.indexOf(cls);
749
+ while (idx !== -1) {
750
+ if ((idx === 0 || cn.charCodeAt(idx - 1) <= 32) && (idx + L === cn.length || cn.charCodeAt(idx + L) <= 32)) return true;
751
+ idx = cn.indexOf(cls, idx + 1);
752
+ }
753
+ return false;
754
+ }
740
755
  function collectByClass(root, classes) {
741
756
  const out = [];
742
757
  const visit = (node) => {
743
- for (const c of node.__children()) {
744
- if (c.nodeType === ELEMENT_NODE) {
745
- if (classes.every((cl) => c.classList.contains(cl))) out.push(c);
746
- visit(c);
747
- }
758
+ const kids = node.__children();
759
+ for (let i = 0; i < kids.length; i++) {
760
+ const c = kids[i];
761
+ if (c.nodeType !== ELEMENT_NODE) continue;
762
+ let ok = true;
763
+ for (let j = 0; j < classes.length; j++) if (!elHasClass(c, classes[j])) { ok = false; break; }
764
+ if (ok) out.push(c);
765
+ visit(c);
748
766
  }
749
767
  };
750
768
  visit(root);
@@ -83,8 +83,17 @@ function parseComplex(src) {
83
83
  return { compounds, combinators };
84
84
  }
85
85
 
86
+ // parsed-selector cache: querySelector(All)/matches re-run the same selector
87
+ // strings constantly; parsing once and reusing is a large win on query-heavy
88
+ // suites. Bounded so a pathological generator can't grow it without limit.
89
+ const __selectorCache = new Map();
86
90
  export function parseSelectorList(selector) {
87
- return splitTopLevel(selector, ',').map((s) => parseComplex(s.trim()));
91
+ const hit = __selectorCache.get(selector);
92
+ if (hit !== undefined) return hit;
93
+ const parsed = splitTopLevel(selector, ',').map((s) => parseComplex(s.trim()));
94
+ if (__selectorCache.size > 10000) __selectorCache.clear();
95
+ __selectorCache.set(selector, parsed);
96
+ return parsed;
88
97
  }
89
98
 
90
99
  function splitTopLevel(s, sep) {
@@ -106,9 +115,10 @@ function elementChildren(node) {
106
115
  }
107
116
 
108
117
  function matchAttr(el, a) {
109
- if (!el.hasAttribute(a.name)) return false;
118
+ const raw = el.getAttribute(a.name); // single lookup (null = absent)
119
+ if (raw === null) return false;
110
120
  if (a.op === null) return true;
111
- const v = el.getAttribute(a.name) ?? '';
121
+ const v = raw;
112
122
  const t = a.value ?? '';
113
123
  switch (a.op) {
114
124
  case '=': return v === t;
@@ -211,11 +221,32 @@ function nextElement(el) {
211
221
  return n || null;
212
222
  }
213
223
 
224
+ // allocation-free "does the class attribute contain this whole-word class"
225
+ function hasClass(cn, cls) {
226
+ if (cn === cls) return true;
227
+ const L = cls.length;
228
+ let idx = cn.indexOf(cls);
229
+ while (idx !== -1) {
230
+ const before = idx === 0 || cn.charCodeAt(idx - 1) <= 32;
231
+ const after = idx + L === cn.length || cn.charCodeAt(idx + L) <= 32;
232
+ if (before && after) return true;
233
+ idx = cn.indexOf(cls, idx + 1);
234
+ }
235
+ return false;
236
+ }
237
+
214
238
  function matchCompound(el, compound) {
215
239
  if (!el || el.nodeType !== 1) return false;
240
+ // cheapest checks first; tag/local is a plain property
216
241
  if (compound.tag && compound.tag !== '*' && el.localName !== compound.tag) return false;
217
242
  if (compound.id !== null && el.getAttribute('id') !== compound.id) return false;
218
- for (const cls of compound.classes) if (!el.classList.contains(cls)) return false;
243
+ // classes: read the class attribute ONCE and test membership without
244
+ // allocating (no ClassList, no split, no padded copy) — dominated matching.
245
+ if (compound.classes.length) {
246
+ const cn = el.getAttribute('class');
247
+ if (!cn) return false;
248
+ for (const cls of compound.classes) if (!hasClass(cn, cls)) return false;
249
+ }
219
250
  for (const a of compound.attrs) if (!matchAttr(el, a)) return false;
220
251
  for (const p of compound.pseudos) if (!matchPseudo(el, p)) return false;
221
252
  return true;
@@ -268,13 +299,43 @@ export function matchesSelector(el, selector) {
268
299
  return list.some((cx) => matchComplex(el, cx));
269
300
  }
270
301
 
302
+ // Fast paths for the overwhelmingly common simple selectors, skipping the
303
+ // parse + per-element matchComplex machinery.
304
+ const SIMPLE = /^\s*(#[\w-]+|\.[\w-]+|[a-zA-Z][\w-]*)\s*$/;
305
+ function simpleMatcher(selector) {
306
+ const m = SIMPLE.exec(selector);
307
+ if (!m) return null;
308
+ const s = m[1];
309
+ if (s[0] === '#') { const id = s.slice(1); return (el) => el.getAttribute('id') === id; }
310
+ if (s[0] === '.') { const cls = s.slice(1); return (el) => el.classList.contains(cls); }
311
+ const tag = s.toLowerCase(); return (el) => el.localName === tag;
312
+ }
313
+
314
+ // child node array without allocating a filtered copy per call
315
+ function rawChildren(node) {
316
+ return typeof node.__children === 'function' ? node.__children() : Array.from(node.childNodes || []);
317
+ }
318
+
271
319
  export function querySelectorAll(root, selector) {
272
- const list = parseSelectorList(selector);
320
+ const simple = simpleMatcher(selector);
273
321
  const out = [];
322
+ if (simple) {
323
+ const visit = (node) => {
324
+ const kids = rawChildren(node);
325
+ for (let i = 0; i < kids.length; i++) { const c = kids[i]; if (c.nodeType !== 1) continue; if (simple(c)) out.push(c); visit(c); }
326
+ };
327
+ visit(root);
328
+ return out;
329
+ }
330
+ const list = parseSelectorList(selector);
331
+ const single = list.length === 1 ? list[0] : null;
274
332
  const visit = (node) => {
275
- for (const child of elementChildren(node)) {
276
- if (list.some((cx) => matchComplex(child, cx))) out.push(child);
277
- visit(child);
333
+ const kids = rawChildren(node);
334
+ for (let i = 0; i < kids.length; i++) {
335
+ const c = kids[i];
336
+ if (c.nodeType !== 1) continue;
337
+ if (single ? matchComplex(c, single) : list.some((cx) => matchComplex(c, cx))) out.push(c);
338
+ visit(c);
278
339
  }
279
340
  };
280
341
  visit(root);
@@ -282,18 +343,29 @@ export function querySelectorAll(root, selector) {
282
343
  }
283
344
 
284
345
  export function querySelector(root, selector) {
346
+ const simple = simpleMatcher(selector);
347
+ if (simple) {
348
+ const visit = (node) => {
349
+ const kids = rawChildren(node);
350
+ for (let i = 0; i < kids.length; i++) { const c = kids[i]; if (c.nodeType !== 1) continue; if (simple(c)) return c; const r = visit(c); if (r) return r; }
351
+ return null;
352
+ };
353
+ return visit(root);
354
+ }
285
355
  const list = parseSelectorList(selector);
286
- let found = null;
356
+ const single = list.length === 1 ? list[0] : null;
287
357
  const visit = (node) => {
288
- for (const child of elementChildren(node)) {
289
- if (found) return;
290
- if (list.some((cx) => matchComplex(child, cx))) { found = child; return; }
291
- visit(child);
292
- if (found) return;
358
+ const kids = rawChildren(node);
359
+ for (let i = 0; i < kids.length; i++) {
360
+ const c = kids[i];
361
+ if (c.nodeType !== 1) continue;
362
+ if (single ? matchComplex(c, single) : list.some((cx) => matchComplex(c, cx))) return c;
363
+ const r = visit(c);
364
+ if (r) return r;
293
365
  }
366
+ return null;
294
367
  };
295
- visit(root);
296
- return found;
368
+ return visit(root);
297
369
  }
298
370
 
299
371
  export const _internal = { parseComplex, parseSelectorList, matchCompound, matchComplex };