React Testing Library is faster when it's part of the DOM

Previously, I made one React Testing Library test file 43% faster by fixing three hot paths in jsdom and DOMSelector. I’ve made a bunch more prs since and most of it shipped in jsdom 30.1.0.

React Testing Library’s API is great. getByRole and friends read like how a person uses the page, and because it only touches the DOM’s public API, the same tests run on jsdom, happy-dom or a real browser. The downside is that it can’t take any shortcuts and has to go through the DOM’s public API for every query.

But we can slop fork anything now, so I had a simple hypothesis:

React Testing Library would be a lot faster on a DOM designed for it, with Testing Library built in.

Results

Each project’s own test suite, unchanged, using jsdom + @ctrl/dom with its own version of React Testing Library:

Projectjsdom@ctrl/domFaster
Sentry138.2 s94.2 s32%
Base UI59.7 s34.5 s42%
React Router17.1 s8.9 s48%
React Hook Form5.9 s2.7 s54%

Why it’s faster

One kind of node

In jsdom, every node is two objects. The div you get back from createElement is a generated wrapper around a hidden implementation object. A call like div.getAttribute('id') goes through the wrapper first:

// jsdom/lib/jsdom/living/generated/Element.js
getAttribute(qualifiedName) {
  if (!exports.is(this)) throw new TypeError(...);
  qualifiedName = conversions.DOMString(qualifiedName);
  return this[implSymbol].getAttribute(qualifiedName);
}

Every window also gets a fresh set of these classes.

@ctrl/dom uses the same plain object shape for every node, NodeImpl. V8 sees one shape and can optimize property access. The classes are also shared between windows. Vitest starts a new worker for every test file by default, so this setup runs once per file.

One object per node instead of two also means less memory, and memory is time in a test run. Every object you allocate is one the garbage collector has to find and walk later, and those pauses land in the middle of your tests. After a first test, jsdom’s heap is 41 MB and @ctrl/dom’s is 16 MB. A Vitest worker finishes a file at 171 MB on jsdom and 111 MB on @ctrl/dom.

Environment setup, per test file

jsdom259 ms
happy-dom147 ms
@ctrl/dom5.13 ms
Mean environment setup per test file, Vitest's default pool, 100 React test files.

Cheating any way we can

When you call getByRole('button', { name: 'Save' }), stock Testing Library runs querySelectorAll('*[role~="button"],input,button'). That grabs every <input> on the page too, so text boxes and checkboxes get checked like they might be buttons. Then for each match it works out three things:

For each matchStock Testing Library@ctrl/dom
Roletries aria-query’s 113 element selectors with matches()one array read by tag id
Namecomputed, even if the element turns out to be hiddenonly for visible matches
HiddengetComputedStyle() on every ancestorcached on the node

The role alone takes 12 matches() calls for a text input, 41 for a button and 96 for a <p>.

@ctrl/dom cheats. Its Testing Library walks the NodeImpl objects directly. Every element gets a numeric tag id when it’s created, and the role tables are arrays indexed by that id, built once when the module loads.

Then it caches everything. Every mutation bumps a document version, and until it changes, query results, accessible names, computed styles and live collections all get reused. It’s the idea from my jsdom label index pr, used everywhere. Stock Testing Library can’t tell whether the DOM changed since the last query, so it redoes all of it every time.

jsdom + Testing Library4.55 ms

Testing Library → aria-query → dom-accessibility-api → getComputedStyle() → jsdom wrapper → jsdom impl

@ctrl/dom + Testing Library798 µs

Testing Library → aria-query → dom-accessibility-api → getComputedStyle() → @ctrl/dom

@ctrl/dom + its Testing Library182 µs

@ctrl/dom/testing-library → NodeImpl

A 20-field form and 10 getByRole queries, warm. The middle row is stock Testing Library on @ctrl/dom.

Both still make one pass over the page, so a query is O(n) either way. The difference is the work per element. To find one input among about 4,000 elements, stock Testing Library makes 12,000 matches() calls and 2,007 getComputedStyle() calls. @ctrl/dom makes none.

getByRole time vs. elements on the page

0 ms10 ms20 ms30 ms01k2k3k4kelements on the page
At 4,004 elementsfirstrepeatmatches()getComputedStyle()
jsdom + Testing Library28.3 ms13.4 ms12,0002,007
@ctrl/dom + its Testing Library0.62 ms0.08 ms00

Solid lines are the first query after a render, dashed lines are the same query repeated.

getByRole for the last field of a form as the form grows, median of 15 runs. Stock is jsdom 31.0.1 with @testing-library/dom 10.4.2.

There was some accidentally quadratic code too. Every *ByRole candidate climbed all of its ancestors to find its visibility, so one getByRole in a tree nested 1,000 deep took 6.1 ms. Now it stops at the nearest ancestor with a style, or skips the climb on a page with no styles, and the same query takes 0.09 ms. Stock getAllByLabelText runs an [id="x"] selector for every element with aria-labelledby, and that selector skipped the id index #x uses. On a page with 10,000 of them, one query went from 720 ms to 14 ms.

Why wait?

On jsdom, timers wait in real time, so findBy*, waitFor and user-event all spend real milliseconds waiting. In Sentry’s suite, 13% of test time was just waiting.

@ctrl/dom runs timers on a virtual clock instead. When nothing is left to run but a timer, it fires that timer now and moves Date.now() and performance.now() forward to match. Timers still fire in order, so the app can’t tell. You can turn it off per project or per file with timeSkip: false.

findByRole after a 20 ms timer

jsdomwaiting on the timer23.3 ms
@ctrl/dom108 µs
Real elapsed time, to scale.
Benchmarkjsdom@ctrl/domFaster
findByRole after a 20 ms timer23.3 ms108 µs216×
user.click a button 10 times53.2 ms249 µs213×
user.type('hello world')22.9 ms285 µs80×

The synthetic numbers

The same 100 React test files, run on each DOM with Vitest, Jest and Bun:

VitestJestBun
jsdom12.59 s2.34 s1.69 s
happy-dom 219.29 s2.17 s1.52 s
@ctrl/dom, stock Testing Library6.55 s 1.9×1.80 s 1.3×465 ms 3.6×
@ctrl/dom + its Testing Library2.42 s 5.2×741 ms 3.2×271 ms 6.2×

You don’t have to use the Testing Library that comes with @ctrl/dom. With the stock packages you only get the faster DOM, which is 1.3–3.6× here. Most of the speedup comes from the Testing Library rewrite, because that’s where the query work happens.

Vitest starts a fresh worker for every file, so it pays environment setup 100 times. Jest reuses its workers, and bun test runs every file in one window. Vitest and Bun run jsdom 31, Jest runs jsdom 26.

Where the time actually goes

Profiling Base UI’s suite on @ctrl/dom:

WhereCPU
Node module loading23.1%
Vitest14.1%
Base UI’s own code14.1%
react-dom11.9%
@ctrl/dom DOM7.3%
@ctrl/dom testing~3%

@ctrl/dom is about 10% of the run. The rest is Node loading modules and React doing React things.

That’s why Sentry gets 32% and not the 5.2× from the synthetic benchmark. Those 100 files are small tests that spend most of their time in the DOM and Testing Library, so making those fast makes the whole run fast. Sentry’s tests import a huge app and render big component trees, and most of that time was never in the DOM. A faster DOM can only cut the part that was the DOM.

What isn’t implemented

@ctrl/dom is built for React component tests and skips what they don’t need: scripts, navigation, fetch, image loading, iframe src and real layout. Node 26+ and React 19 only. If you need any of that, use jsdom.

Trying it

pnpm add -D @ctrl/dom
// vitest.config.ts
import { defineConfig } from 'vitest/config';

import { ctrlDom } from '@ctrl/dom/vitest';

export default defineConfig({
  plugins: [ctrlDom()],
});

The plugin points your @testing-library/* imports at the @ctrl/dom versions. Pass ctrlDom({ testingLibrary: false }) to keep the stock packages and only swap the DOM. It’s slower, but it’s an easy first step if your tests depend on stock Testing Library behavior. Jest setup is in the README.

Or hand it to your coding agent.

Switch this project's tests from jsdom to @ctrl/dom following https://github.com/scttcper/dom#setup. Run the suite before and after, and list any tests that only fail on @ctrl/dom.

It’s 0.x and almost certainly broken in some way your suite will find. Please open an issue when it does. I want to add more projects to the downstream suites.

My next question is, when will bun build this into its own testing environment to beat the competition.

A clerk under the last working light holds up one index card while gray figures wait in the dark aisles of filing cabinets
AI-generated image of a clerk who already knows which drawer it's in