Skip to content

Build the QSA wrapper argument list in one allocation - #187

Open
jdalton wants to merge 1 commit into
dperini:masterfrom
jdalton:perf/wrapper-arguments
Open

Build the QSA wrapper argument list in one allocation#187
jdalton wants to merge 1 commit into
dperini:masterfrom
jdalton:perf/wrapper-arguments

Conversation

@jdalton

@jdalton jdalton commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

The wrappers install() puts on the DOM prototypes forward their arguments as a slice concatenated with the resolver, which allocates twice for a call carrying at most three arguments. Sizing the list by arity in one allocation is 1.85x on an installed querySelector for a cheap selector, and invisible on an expensive one.

Detail, and how it was checked

The wrappers install() puts on the DOM prototypes forward their arguments to parseQSArgs as [].slice.call(arguments).concat(resolver), which allocates twice for a call that carries at most three arguments. argsWith() sizes the list by arity in a single allocation, unrolled to eight and falling through to the general form beyond that.

Measured in isolation, building the list drops from ~113ns to ~9ns, and with the apply included from ~119ns to ~14ns.

End to end in Chromium, three rounds with the order swapped, an installed querySelector(#root) against a 200-element document:

round 1 round 2 round 3
before 175 ns 174 ns 168 ns
after 95 ns 92 ns 90 ns
before, the wrapper alone 98 ns 97 ns 94 ns
after, the wrapper alone 19 ns 17 ns 17 ns

That is 1.85x on a cheap query, where fixed overhead is most of the call and the wrapper was 56% of it. On an expensive one it disappears into the query: the same change against a 200-match p.x is ~1% of 11.7us and not separable from run-to-run noise.

Extracted from #167 as a standalone change: one file, applies to master on its own, and checked against the benchmark fixture to confirm every selector still agrees with the native engine.

References: the spec, the browser source, and what each part was reasoned from

This patch applies to master on its own. The sixteen in this series were checked by cherry-picking them onto master one after another, in this order and in reverse, and all sixteen land without a conflict.

  • MDN: arguments — the arguments object these wrappers forward.

@jdalton
jdalton force-pushed the perf/wrapper-arguments branch from 924f62f to 7738820 Compare September 4, 2026 18:00
The wrappers install() puts on the DOM prototypes forward their arguments to parseQSArgs as [].slice.call(arguments).concat(resolver), which allocates twice for a call that carries at most three arguments. argsWith() sizes the list by arity in a single allocation, unrolled to eight and falling through to the general form beyond that.
 Measured in isolation, building the list drops from ~113ns to ~9ns, and with the apply included from ~119ns to ~14ns.
 End to end in Chromium, three rounds with the order swapped, an installed querySelector('#root') against a 200-element document:
 before 175ns 174ns 168ns (wrapper 98ns, 97ns, 94ns) after 95ns 92ns 90ns (wrapper 19ns, 17ns, 17ns)
 That is 1.85x on a cheap query, where fixed overhead is most of the call and the wrapper was 56% of it. On an expensive one it disappears into the query: the same change against a 200-match 'p.x' is ~1% of 11.7us and not separable from run-to-run noise.

References:

- MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments — the arguments object these wrappers forward
@jdalton
jdalton force-pushed the perf/wrapper-arguments branch from 7738820 to 8f7780c Compare September 5, 2026 02:48
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