Skip to content

Comments

perf(parser): fast-path name tokenization to avoid TextDecoder#33

Merged
Mythie merged 1 commit intoLibPDF-js:mainfrom
ntedvs:perf/fast-path-name-tokenization
Feb 22, 2026
Merged

perf(parser): fast-path name tokenization to avoid TextDecoder#33
Mythie merged 1 commit intoLibPDF-js:mainfrom
ntedvs:perf/fast-path-name-tokenization

Conversation

@ntedvs
Copy link
Contributor

@ntedvs ntedvs commented Feb 21, 2026

Summary

  • Add fast path in readName() that avoids number[] accumulation, Uint8Array allocation, and TextDecoder.decode() for names without #XX hex escapes (99%+ of cases)
  • Build string directly from byte range via String.fromCharCode loop
  • Fall back to existing slow path (unchanged) when # escapes are present

Benchmarks (5-run average)

Benchmark Change
get form fields +10.2%
load small PDF +8.2%
extract 1p from 100p +6.0%
merge 2x100-page +5.8%
read field values +5.4%
load medium PDF +4.8%
load, modify, save +4.5%
0 regressions

Motivation

CPU profiling showed readName + TextDecoder.decode consuming ~20% of total parse time. PDF name tokens (/Type, /Page, /MediaBox) are overwhelmingly pure ASCII with no hex escapes.

Test plan

  • 3 new tests covering fast/slow path boundaries
  • All 120 token-reader tests pass
  • Full suite (2867 tests) passes

Skip intermediate number[] array, Uint8Array allocation, and
TextDecoder.decode() for the 99%+ of PDF names that contain no #XX
hex escapes. Build string directly via String.fromCharCode loop.

5-10% improvement on parsing benchmarks (CPU profile showed readName +
TextDecoder.decode at ~20% of total parse time).
@vercel
Copy link
Contributor

vercel bot commented Feb 21, 2026

@ntedvs is attempting to deploy a commit to the mythie's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Mythie
Copy link
Contributor

Mythie commented Feb 22, 2026

LGTM!

@Mythie Mythie merged commit 82ce95b into LibPDF-js:main Feb 22, 2026
3 of 5 checks passed
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.

2 participants