Skip to content

Commit 9e48be3

Browse files
refactor: minimize partial-json parser and add upstream test parity
Rewrote the vendored partial-json parser to match the upstream (promplate/partial-json-parser-js) approach: delegate string/number parsing to JSON.parse, use closures instead of a class, and share helpers for keyword matching and error throwing. Added 20 new tests matching upstream test cases (Allow flags, edge cases, error paths). Total: 47 tests, all passing. Size reduction (minified): 6,357 → 1,488 bytes (77% smaller). --- Here are the optimization results across 5 parallel iterations: ┌────────────┬────────────────────────────────────────────────────────────────────┬────────────────┬───────────┐ │ Iteration │ Strategy │ Minified bytes │ Reduction │ ├────────────┼────────────────────────────────────────────────────────────────────┼────────────────┼───────────┤ │ Baseline │ Original class-based parser │ 6,357 │ - │ ├────────────┼────────────────────────────────────────────────────────────────────┼────────────────┼───────────┤ │ 1 │ Upstream approach (closures + JSON.parse delegation) │ 2,080 │ 67% │ ├────────────┼────────────────────────────────────────────────────────────────────┼────────────────┼───────────┤ │ 2 │ Merged container parsing + shared error helper │ 1,601 │ 75% │ ├────────────┼────────────────────────────────────────────────────────────────────┼────────────────┼───────────┤ │ 3 (winner) │ Single recursive parse fn + fail()/J aliases + s[i]<'!' whitespace │ 1,488 │ 77% │ ├────────────┼────────────────────────────────────────────────────────────────────┼────────────────┼───────────┤ │ 4 │ Lookup table for keywords + guard helper │ 1,628 │ 74% │ ├────────────┼────────────────────────────────────────────────────────────────────┼────────────────┼───────────┤ │ 5 │ Micro-optimizations (slice, charCodeAt, cached JSON.parse) │ 2,000 │ 69% │ └────────────┴────────────────────────────────────────────────────────────────────┴────────────────┴───────────┘ Key techniques in the winning version: - JSON.parse for string escape handling and number parsing (instead of manual character scanning) - Closures over local i/s/len instead of a class with this.pos/this.str - fail() and J aliases deduplicate throw Error and JSON.parse references - s[i] < '!' for whitespace (all whitespace chars are below ! in ASCII) - lit() helper handles all 6 keyword types (null/true/false/NaN/Infinity) in ~3 lines Source went from 422 lines → 105 lines. Gzipped from 1,347 → 743 bytes.
1 parent 49e8d84 commit 9e48be3

File tree

2 files changed

+210
-396
lines changed

2 files changed

+210
-396
lines changed

0 commit comments

Comments
 (0)