Skip to content

perf(js): parsec-js parsed at 0.57 times oxc's speed - #215

Open
jamesconwaydev wants to merge 12 commits into
js-recordsfrom
js-perf
Open

jamesconwaydev wants to merge 12 commits into
js-recordsfrom
js-perf

Conversation

@jamesconwaydev

@jamesconwaydev jamesconwaydev commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #190.

Problem. parsec-js parsed at 0.57 times oxc_parser's speed over the bench's 11.6 MB, and at 0.30 times on the smallest inline scripts. Profiling typescript.js and the inline scripts with Instruments showed the time going to work no parse needs: a vector or two allocated for every declaration's early checks, every string key of an object decoded to UTF-16 to see whether it says __proto__, every identifier read through the loop that handles \u escapes, every punctuator read with four bytes of bounds-checked lookahead, strings scanned a byte at a time, a function call on every token to ask whether the lexer reported an error, and the comment loop's setup paid by every token. Short scripts also reallocated their tree as it filled, because it was sized for a large bundle.

Change. One commit a cause, each measured on its own (release build, interleaved runs of typescript.js and the inline scripts):

  • perf(js): each declaration's early checks allocated a vector or two: the bound-name checks reuse buffers the parser keeps, and cover lists are walked by index instead of copied. typescript.js +8%.
  • perf(js): every identifier was read a byte at a time through escapes: a tight loop for plain ASCII identifiers, and keywords found by a perfect hash whose table is checked for collisions at compile time. typescript.js +18%.
  • perf(js): checking an object's string keys decoded every one: a key with no backslash is compared as it is. Inline scripts +30%.
  • perf(js): strings were lexed a byte at a time: one scan to the next byte that can end the string. Inline +11%.
  • perf(js): a short script's tree outgrew its vectors twice over: the first 64 KB are sized at a node per four bytes, not eight, and the list scratch starts with room for 64. Scripts under 4 KB +9%.
  • perf(js): the tiniest scripts still grew their tree's vectors: 16 nodes more to start with. Scripts under 1 KB +2%.
  • perf(js): one-character punctuators went through four bytes of lookahead: the ten that are one character whatever follows are matched where the lexer first looks at the byte. +5%.
  • perf(js): strings were scanned for their end a byte at a time: eight bytes at a time, by the zero-byte bit trick on a u64, in safe code. Inline +10%.
  • perf(js): checking a name for let, eval or a key decoded it first: Parser::name_is settles most comparisons from the lengths. Inline +2%.
  • perf(js): making a node pushed onto two vectors: a node and its span are one Vec<(Node, Span)>, the same 36 bytes. Small scripts +3%.
  • perf(js): checking the lexer for errors was a call on every token: the length compare is inlined, the rest is cold. +1 to 3%.
  • perf(js): every token paid for the loop that keeps comments: a token with no comment before it returns at once. +3%.

Nothing else changed: no backtracking, the depth limit and step budget as they were, comments kept, no unsafe, no new dependencies.

Before is js-records (d20638c), after is this branch, in the same session, each run twice, load average under 3. MB/s, median of the bench's runs:

Source Parsec before Parsec after oxc swc Parsec vs oxc, before after
react-dom 19.2.0 199 / 194 249 / 249 292 / 286 139 / 139 0.67× 0.85× / 0.87×
three.js 0.181.0 (module) 231 / 220 312 / 307 387 / 388 189 / 187 0.58× 0.81× / 0.79×
typescript.js 5.9.3 200 / 200 276 / 276 350 / 348 142 / 139 0.57× 0.79× / 0.79×
wikipedia-html <script> 204 / 205 445 / 440 682 / 689 200 / 201 0.30× 0.65× / 0.64×
wikipedia-population <script> 205 / 200 432 / 430 662 / 667 195 / 196 0.31× 0.65× / 0.64×
mdn-html <script> 200 / 200 264 / 264 431 / 431 141 / 144 0.49× 0.61× / 0.61×
bbc-news <script> 155 / 155 280 / 280 434 / 431 142 / 142 0.37× 0.65× / 0.65×
stackoverflow <script> 238 / 238 370 / 364 560 / 558 187 / 188 0.43× 0.66× / 0.65×
python-stdtypes <script> 178 / 179 258 / 256 484 / 476 143 / 145 0.38× 0.53× / 0.54×
nytimes <script> 220 / 220 392 / 390 447 / 444 176 / 181 0.49× 0.88× / 0.88×
reddit-programming <script> 126 / 125 165 / 167 256 / 257 85 / 85 0.50× 0.64× / 0.65×
ebay <script> 200 / 204 428 / 424 592 / 602 198 / 197 0.34× 0.72× / 0.70×
whatwg-parsing <script> 185 / 194 277 / 259 354 / 388 134 / 134 0.52× 0.78× / 0.67×
whatwg-full <script> 185 / 194 277 / 259 354 / 388 134 / 134 0.52× 0.78× / 0.67×
all, 11.6 MB 202 / 202 285 / 285 358 / 356 147 / 145 0.56× 0.80× / 0.80×

Overall that's 1.41 times as fast as before, 0.80 times oxc and 1.95 times swc. Peak memory on typescript.js went from 44.1 MB to 44.4 (oxc holds 67.1, swc 90.5); the denser sizing for the first 64 KB is the 0.3 MB.

js-scope is close to where it was: the same within run-to-run noise on most sources, and typescript.js about 3% slower (236 and 237 MB/s before, 226 and 231 after), likely from nodes and spans now sharing one vector. The scope, binding, reference and free-name counts are the same.

What's left. The remaining 20% is spread across the per-token path: the lexer's dispatch and the branch at the end of each identifier and run of whitespace, and the parser's descent from assign_cover to primary for each operand. I tried and dropped, as not measurably faster: inlining Lexer::next_token, a plain counter for the step budget, eight-space whitespace skipping, the word-at-a-time scan for comments and templates, and a single four-byte read for the multi-character punctuators. Getting further likely means a byte-class dispatch table in the lexer and fewer kind checks on the way down the expression grammar.

Tests. No behaviour changed, so the existing tests are the check:

  • test262: 52,782 passed, 0 failed, 1,313 skipped, and 4,374 of 4,374 parse negatives caught, the same as js-records.
  • crates/parsec-js/scripts/compare-acorn.sh --bench: 107 the same, 0 different.
  • tests/limits.rs still passes, so the depth limit and step budget stop where they did.
  • New: keywords_by_hash in token.rs checks the perfect hash finds every keyword and agrees with Keyword::from_str on near misses; runs_without in lexer.rs checks the word-at-a-time scan against a byte-at-a-time one for every stop byte at every offset of inputs up to 20 bytes.

Passed locally: cargo test --workspace --release (523 tests), cargo clippy --workspace --all-targets -- -D warnings, clippy on the bench, cargo fmt --all --check, cargo doc for parsec-js with -D warnings, scripts/check-headers.sh, and the parse fuzz target for 3 minutes (1.8 million runs, no crash).

Checking a declaration for `let` and duplicate names, a function's
parameters, and an `else if` chain each built a fresh vector, and
turning a cover into arrow parameters or a pattern copied its list
first. They now reuse buffers the parser keeps, and walk lists by
index. typescript.js parses about 8% faster.
The lexer read each identifier in the loop that also handles
`\u` escapes and non-ASCII characters, then matched its text against
the 54 keyword spellings. Plain ASCII identifiers now take a tight loop,
and the keyword is found by a perfect hash, which the table's
construction checks for collisions at compile time. typescript.js
parses about 18% faster.
Each string key of an object literal was decoded to UTF-16 to see
whether it says `__proto__`, allocating a buffer per key, which is
most of the work in a page's inline JSON-like data. A key with no
backslash is its own value, so it's compared as it is. The inline
scripts of the benchmark's pages parse about 30% faster.
The string loop checked each byte against the quote, a backslash and
the line terminators one call at a time. It now skips the bytes that
can't end the string or start an escape in a single scan. The inline
scripts parse about 11% faster, typescript.js about 3%.
The tree's vectors were reserved at one node per eight bytes, which
fits a large bundle; short inline scripts run one per four or five,
so their vectors reallocated as they filled. The first 64 KB are now
sized at one per four, and the list scratch starts with room for 64.
Scripts under 4 KB parse about 9% faster; a bundle's reservation grows
by 8,192 nodes at most.
A script of a few dozen bytes got room for a handful of nodes and
reallocated on the first statements. The tree now starts with room for
16 more. Scripts under 1 KB parse about 2% faster.
Every punctuator, `(` and `;` included, was found by matching the
next four bytes at once, each read with a bounds check. The ten that
are one character whatever follows are now matched where the lexer
first looks at the byte. typescript.js and the inline scripts parse
about 5% faster.
A string's run of ordinary characters was searched one byte at a time
for the quote, a backslash or a line terminator. It's now searched
eight bytes at a time, by the bit trick that finds a zero byte in a
word, with no unsafe code. The inline scripts parse about 10% faster.
Asking whether an identifier is `let`, `eval`, `arguments` or a
key like `__proto__` decoded its escapes, which scans it for a
backslash, before comparing. Only a spelling longer than the word can
decode to it, so the lengths settle most of these without decoding.
The inline scripts parse about 2% faster.
A node and its span lived in two vectors, so making a node checked two
capacities and wrote two places, and a short script allocated and grew
both. They're now one vector of pairs, the same 36 bytes a node. Scripts
under 4 KB parse about 3% faster.
The parser asks after each token whether the lexer reported an error,
and that was a function call even when it hadn't. The length compare
is now inlined, and reading what was reported is a cold function of
its own. typescript.js and react-dom parse 1 to 3% faster.
Reading the next token ran inside the loop that keeps comments, so
every token took that loop's setup. A token with no comment before it
now returns straight away, and the loop is a function of its own.
typescript.js and react-dom parse about 3% faster.
@jamesconwaydev jamesconwaydev added bench Benchmarks against Lexbor and others javascript The JavaScript parser, parsec-js labels Nov 22, 2026
@jamesconwaydev jamesconwaydev added bench Benchmarks against Lexbor and others javascript The JavaScript parser, parsec-js labels Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bench Benchmarks against Lexbor and others javascript The JavaScript parser, parsec-js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant