perf(json): avoid first-char regex for direct JSON bodies#744
Open
Phillip9587 wants to merge 1 commit into
Open
perf(json): avoid first-char regex for direct JSON bodies#744Phillip9587 wants to merge 1 commit into
Phillip9587 wants to merge 1 commit into
Conversation
Skip the RFC 7159 whitespace scan when strict JSON bodies start directly with `{` or `[`. This avoids regex execution on the common valid path while preserving strict-mode handling for leading whitespace and primitive values.
Update the first non-whitespace regex to match end-of-string so whitespace-only bodies return `undefined` without relying on a failed match/backtracking path.
UlisesGascon
approved these changes
Jul 7, 2026
Member
|
Could you tell us how much of an improvement there is? |
Member
Author
|
Micro Benchmark shows:
The load test from perf-wg do not show a big difference because http-handling, streaming and json parsing take much longer. |
bjohansebas
reviewed
Jul 9, 2026
| * %x0D ) ; Carriage return | ||
| */ | ||
| const FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex | ||
| const FIRST_NON_WHITESPACE_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d]|$)/ // eslint-disable-line no-control-regex |
Member
There was a problem hiding this comment.
Okay, for future reviewers, here's the regex change.
" {json}"
master=match[1]="{"
PR=match[1]="{"
"123" (primitivo)
master=match[1]="1"
PR=match[1]="1"
" " (solo whitespace)
master=NULL (no match)
PR=match[1]=""
"" (vacio)
master=NULL (no match)
PR=match[1]=""
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.
Skip the RFC 7159 whitespace scan when strict JSON bodies start directly with
{or[. This avoids regex execution on the common valid path while preserving strict-mode handling for leading whitespace and primitive values.Updated the first non-whitespace regex to match end-of-string so whitespace-only bodies return
undefinedwithout relying on a failed match/backtracking path.