@miaskiewicz/turbo-dom 0.1.16 → 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 +12 -9
- package/package.json +5 -3
- package/src/runtime/dom.mjs +30 -12
- package/src/runtime/selectors.mjs +88 -16
- package/turbo-dom-parser.win32-x64-msvc.node +0 -0
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** — ~
|
|
17
|
+
- ⚡ **Faster than jsdom** — ~23× lower per-file setup, ~6× on query-heavy DOM work, 18–37× 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) | **
|
|
108
|
-
| full suite, 200 files (ms/file) | **0.13** | 1.
|
|
109
|
-
|
|
|
110
|
-
| parse
|
|
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;
|
|
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,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miaskiewicz/turbo-dom",
|
|
3
|
-
"version": "0.1.
|
|
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
|
+
"license": "MIT",
|
|
5
6
|
"main": "index.js",
|
|
6
7
|
"types": "index.d.ts",
|
|
7
8
|
"exports": {
|
|
@@ -63,10 +64,11 @@
|
|
|
63
64
|
"bench:construct": "node bench/construct.mjs",
|
|
64
65
|
"bench:suite": "node bench/suite.mjs",
|
|
65
66
|
"bench:wasm": "node bench/wasm.mjs",
|
|
66
|
-
"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",
|
|
67
68
|
"prepublishOnly": "napi build --platform --release",
|
|
68
69
|
"test:vitest": "vitest run -c test-vitest/vitest.config.mjs",
|
|
69
|
-
"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"
|
|
70
72
|
},
|
|
71
73
|
"keywords": [
|
|
72
74
|
"dom",
|
package/src/runtime/dom.mjs
CHANGED
|
@@ -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
|
|
304
|
-
hasAttribute(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
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
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
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
276
|
-
|
|
277
|
-
|
|
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
|
-
|
|
356
|
+
const single = list.length === 1 ? list[0] : null;
|
|
287
357
|
const visit = (node) => {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
if (
|
|
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 };
|
|
Binary file
|