I asked an LLM to analyze the current code quality.
There where 7 specific recommendations. Part two got sub-parts. Part 2.2:
2.2 Parser Performance
Current state:
parser.js uses brace-matching with string traversal for JSON extraction — O(n) but processes the entire response text.
Optimization opportunities:
Early exit: The parser currently checks for raw JSON first, then JSON code blocks, then XML. If the AI consistently outputs one format, cache the detected format and skip other checks.
Regex compilation: Some regex patterns in parser.js and jsonRepair.js are compiled on every call. Pre-compile them as module-level constants.
Response size: The parser processes the full response including thinking tags. The thinking tag removal happens before parsing, but the response text could be truncated to the relevant portion earlier in the pipeline.
I asked an LLM to analyze the current code quality.
There where 7 specific recommendations. Part two got sub-parts. Part 2.2:
### 2.2 Parser Performance
Current state:
parser.js uses brace-matching with string traversal for JSON extraction — O(n) but processes the entire response text.
**Optimization opportunities:**
- Early exit: The parser currently checks for raw JSON first, then JSON code blocks, then XML. If the AI consistently outputs one format, cache the detected format and skip other checks.
- Regex compilation: Some regex patterns in parser.js and jsonRepair.js are compiled on every call. Pre-compile them as module-level constants.
- Response size: The parser processes the full response including thinking tags. The thinking tag removal happens before parsing, but the response text could be truncated to the relevant portion earlier in the pipeline.
ARIA
self-assigned this 2026-07-12 11:36:04 +00:00
✅ 1. Regex pre-compilation: All regex patterns in parser.js (~50) and jsonRepair.js (14) are now module-level constants compiled once at module load, not on every function call.
✅ 2. Format detection caching: Added lastDetectedFormat cache with hit threshold (3 consecutive hits). Skips redundant checks once format is consistently detected. clearFormatCache() exported for invalidation.
✅ 3. Thinking tag removal: Already happens early in the pipeline before parsing (confirmed optimal placement).
Tests: 33/33 passing covering all parser functions, regex patterns, and format cache logic.
All three optimizations implemented:
✅ 1. Regex pre-compilation: All regex patterns in parser.js (~50) and jsonRepair.js (14) are now module-level constants compiled once at module load, not on every function call.
✅ 2. Format detection caching: Added lastDetectedFormat cache with hit threshold (3 consecutive hits). Skips redundant checks once format is consistently detected. clearFormatCache() exported for invalidation.
✅ 3. Thinking tag removal: Already happens early in the pipeline before parsing (confirmed optimal placement).
Tests: 33/33 passing covering all parser functions, regex patterns, and format cache logic.
PR: https://gitea.zephyre.one/Pakobbix/rpg-companion-sillytavern/pulls/10
Issue auto-closes on merge.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
I asked an LLM to analyze the current code quality.
There where 7 specific recommendations. Part two got sub-parts. Part 2.2:
2.2 Parser Performance
Current state:
parser.js uses brace-matching with string traversal for JSON extraction — O(n) but processes the entire response text.
Optimization opportunities:
I'll work on this. Give me a moment to inspect the codebase and prepare a fix.
All three optimizations implemented:
✅ 1. Regex pre-compilation: All regex patterns in parser.js (~50) and jsonRepair.js (14) are now module-level constants compiled once at module load, not on every function call.
✅ 2. Format detection caching: Added lastDetectedFormat cache with hit threshold (3 consecutive hits). Skips redundant checks once format is consistently detected. clearFormatCache() exported for invalidation.
✅ 3. Thinking tag removal: Already happens early in the pipeline before parsing (confirmed optimal placement).
Tests: 33/33 passing covering all parser functions, regex patterns, and format cache logic.
PR: #10
Issue auto-closes on merge.