You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* feat: improve text parsing performance
From AI:
1. **Escape Checking**: The code rechecks all trailing backslashes in the accumulated `data` string for every character, which becomes increasingly expensive as `data` grows.
2. **String Slicing**: When an escape character is found, `data = data.slice(0, -1)` creates a new string by copying almost the entire accumulated text.
3. **Repeated String Concatenation**: Adding to `data` one character at a time with `data += char` creates a new string object each time.
4. **Regular Expression Replacements**: At the end, it uses complex regex replacements that need to scan the entire string.
5. **Repeated Delimiter Checking**: For each character, it checks against multiple delimiters with `template.startsWith()`, which involves multiple string comparisons.
Our optimization approach for the `text` method focused on several improvements:
1. **Simplified Escape Handling**:
- Instead of recounting backslashes for every character, we now use a boolean flag `isEscaped` to track whether the current character is escaped
- When we encounter a backslash, we toggle the escape state and skip adding it to the data string directly
- This eliminates the expensive backslash counting operation that grew in cost with the size of the text
2. **Eliminated String Slicing**:
- We no longer use `data.slice(0, -1)` which was creating a new string by copying almost the entire accumulated text
- Instead, we simply don't add the backslash to the data string at all
3. **More Efficient Processing Flow**:
- The code now has a clearer path for handling escaped characters
- We continue to the next iteration immediately after processing an escape sequence, reducing unnecessary checks
4. **Simplified Data Processing**:
- We removed the complex regular expression replacements at the end
- Since we're already handling escape sequences correctly during parsing, we don't need additional processing
* feat: expose parser API (#29)
Parsing is an expensive computation, we now expose it to the client and
accept the ast as an argument in both scan method and Chain constructor,
so that clients can optimize performance.
* chore: add CD
0 commit comments