Skip to content

Replace Chevrotain parser with 42x faster single-pass parser#37

Merged
EvanBacon merged 3 commits into
mainfrom
@evanbacon/fast-parser
Feb 27, 2026
Merged

Replace Chevrotain parser with 42x faster single-pass parser#37
EvanBacon merged 3 commits into
mainfrom
@evanbacon/fast-parser

Conversation

@EvanBacon
Copy link
Copy Markdown
Owner

Summary

  • Replace Chevrotain-based parser with hand-optimized single-pass parser
  • 11.7x faster than the legacy xcode npm package
  • 42x faster than the previous Chevrotain implementation
  • Add benchmark suite (bun run bench)

Performance

Metric Before (Chevrotain) After Improvement
Throughput 7.5 MB/s 315 MB/s 42x
react-native (29KB) 2.47ms 120µs 20x
swift-protobuf (263KB) 33ms 800µs 41x

vs Legacy xcode Package

Parser Time (29KB) Notes
@bacons/xcode 120µs Works on all files
legacy xcode 1.41ms Crashes on complex files

Key Optimizations

  1. Single-pass parsing - No CST intermediate representation
  2. Lookup tables - Pre-computed character classification (Uint8Array)
  3. Char code comparisons - Avoid string operations
  4. Fast path for simple strings - Skip escape processing when not needed
  5. Direct object construction - No visitor pattern or reduce/spread overhead

Additional Improvements

  • Better error messages with line and column numbers
  • Handles all edge cases that crash the legacy xcode package
  • Spec-compliant Unicode escape handling (NextStep mappings)

Test plan

  • All 84 JSON parser tests pass
  • Round-trip tests (parse → build → equals original)
  • Benchmark against legacy xcode package
  • Error message tests with line/column verification

🤖 Generated with Claude Code

EvanBacon and others added 3 commits February 27, 2026 12:20
This replaces the Chevrotain-based parser with a hand-optimized single-pass
parser that is 11.7x faster than the legacy xcode package and 42x faster
than the previous Chevrotain implementation.

Performance improvements:
- Throughput: 7.5 MB/s → 315 MB/s (42x improvement)
- react-native fixture (29KB): 2.47ms → 120µs (20x faster)
- swift-protobuf fixture (263KB): 33ms → 800µs (41x faster)

Key optimizations:
- Single-pass parsing without CST intermediate representation
- Pre-computed lookup tables (Uint8Array) for character classification
- Char code comparisons instead of string operations
- Fast path for strings without escape sequences
- Direct object construction without visitor pattern overhead

Additional improvements:
- Better error messages with line and column numbers
- Handles all edge cases that crash the legacy xcode package
- Added benchmark suite (bun run bench)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add performance comparison table showing 11x speedup vs legacy xcode
- Document benchmark command (bun run bench)
- Update solution section to reflect new parser architecture
- Check off benchmarks in TODO list

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@EvanBacon EvanBacon force-pushed the @evanbacon/fast-parser branch from 7721188 to 4cf05f9 Compare February 27, 2026 20:22
@EvanBacon EvanBacon merged commit bd6455d into main Feb 27, 2026
1 check passed
@EvanBacon EvanBacon deleted the @evanbacon/fast-parser branch February 27, 2026 20:24
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