Short-circuit inline parsing plain text#149
Merged
dillonkearns merged 4 commits intomasterfrom Feb 6, 2026
Merged
Conversation
…d expensive parsing for stretches of plain text.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds an early-exit optimization to the inline parser that skips expensive tokenization when text contains no special markdown characters. This speeds up parsing for plain text content.
Changes
hasAnyTokenCharusesString.anyto efficiently check if text contains any markdown-relevant characters (`,*,_,~,[,],<,>,\n)[]immediately, avoiding 8+ separate regex/pattern scansString.containschecks gate each tokenizerWhy multiple
String.containsinstead of single-pass?I explored two alternative "cleaner" approaches and benchmarked them:
Alternative 1: Single-pass
String.foldlBuild all character flags in one pass instead of multiple
String.containscalls:Result: Slightly slower (0.080ms vs 0.078ms for plain text)
Alternative 2: Single-pass recursive tokenizer
Replace all regex-based tokenizers with a single character-by-character scan:
Result: Significantly slower (0.258ms vs 0.078ms for plain text — 3.3x regression)
Why native operations win
Multiple
String.containscalls are actually faster than manual single-pass approaches because:String.containscompiles to JavaScript's nativeindexOf, which is heavily optimized at the engine levelRegex.findsimilarly uses the browser's native regex engineThe "inelegant" multiple-pass approach leverages these native optimizations, making it faster than conceptually cleaner single-pass alternatives.
Performance Impact
Plain text (no formatting): ~1.5x faster
Long unformatted lines: ~2x faster
Formatted content: No regression
Benchmarking
You can verify the results by running the benchmark script:
cd spec-tests npx elm make OutputMarkdownHtml.elm --optimize --output elm.js node benchmark.jsSample results (on my machine):