Skip to content

perf(json): avoid first-char regex for direct JSON bodies - #744

Merged
Phillip9587 merged 1 commit into
expressjs:masterfrom
Phillip9587:improve-json-strict-mode
Aug 13, 2026
Merged

Phillip9587 merged 1 commit into
expressjs:masterfrom
Phillip9587:improve-json-strict-mode

Conversation

@Phillip9587

Copy link
Copy Markdown
Member

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 undefined without relying on a failed match/backtracking path.

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.
@bjohansebas

Copy link
Copy Markdown
Member

Could you tell us how much of an improvement there is?

@Phillip9587

Copy link
Copy Markdown
Member Author

Micro Benchmark shows:

case test json iterations old median ns new median ns delta speedup
direct object "{"hello":"world","n":123,"ok":true}" 2000000 239.586 210.294 -12.23% 1.139x
direct array "[1,2,3,4,5,6,7,8,9,10]" 2000000 168.300 128.147 -23.86% 1.313x
leading whitespace object " \n\t\r {"hello":"world","n":123,"ok":true}" 1000000 254.757 269.237 +5.68% 0.946x
strict violation primitive "true" 200000 9609.080 9533.889 -0.78% 1.008x
whitespace only " \n\t\r" 200000 9138.759 9127.327 -0.13% 1.001x

The load test from perf-wg do not show a big difference because http-handling, streaming and json parsing take much longer.

Comment thread lib/types/json.js
* %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

@bjohansebas bjohansebas Jul 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]=""

@bjohansebas bjohansebas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Phillip9587
Phillip9587 merged commit e528990 into expressjs:master Aug 13, 2026
17 checks passed
@Phillip9587
Phillip9587 deleted the improve-json-strict-mode branch August 13, 2026 21:43
@UlisesGascon UlisesGascon mentioned this pull request Oct 21, 2026
markharding pushed a commit to futo-org/Harbor that referenced this pull request Oct 27, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [body-parser](https://github.com/expressjs/body-parser) | [`2.3.0` → `2.4.0`](https://renovatebot.com/diffs/npm/body-parser/2.3.0/2.4.0) | ![age](https://developer.mend.io/api/mc/badges/age/npm/body-parser/2.4.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/body-parser/2.3.0/2.4.0?slim=true) |

---

> ⚠️ **Warning**
>
> Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/99) for more information.

---

### Release Notes

<details>
<summary>expressjs/body-parser (body-parser)</summary>

### [`v2.4.0`](https://github.com/expressjs/body-parser/blob/HEAD/HISTORY.md#240)

[Compare Source](expressjs/body-parser@v2.3.0...v2.4.0)

\=====

- Security fix for [GHSA-27r3-jxmh-p828](GHSA-27r3-jxmh-p828) (CVE-2026-111181)
- feat: add `zstd` content-encoding support ([#&#8203;586](expressjs/body-parser#586))
  - Requires Node.js `^22.15.0` or `>=23.8.0`; older versions keep returning `415` for `zstd` encoded bodies
- fix: avoid mutating the parser options and the request object while reading the body ([#&#8203;742](expressjs/body-parser#742), [#&#8203;750](expressjs/body-parser#750))
- perf(json): avoid first-char regex for direct JSON bodies ([#&#8203;744](expressjs/body-parser#744))
- deps:
  - content-type@^2.1.0
  - iconv-lite^0.7.3
  - qs@^6.16.1

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - "before 9am on tuesday"
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xNzUuMSIsInVwZGF0ZWRJblZlciI6IjQ0LjE3NS4xIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiLCJyZW5vdmF0ZSJdfQ==-->

Reviewed-on: https://code.futo.org/harbor/harbor/pulls/2043
markharding pushed a commit to futo-org/Harbor that referenced this pull request Oct 29, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [body-parser](https://github.com/expressjs/body-parser) | [`2.3.0` → `2.4.0`](https://renovatebot.com/diffs/npm/body-parser/2.3.0/2.4.0) | ![age](https://developer.mend.io/api/mc/badges/age/npm/body-parser/2.4.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/body-parser/2.3.0/2.4.0?slim=true) |

---

> ⚠️ **Warning**
>
> Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/99) for more information.

---

### Release Notes

<details>
<summary>expressjs/body-parser (body-parser)</summary>

### [`v2.4.0`](https://github.com/expressjs/body-parser/blob/HEAD/HISTORY.md#240)

[Compare Source](expressjs/body-parser@v2.3.0...v2.4.0)

\=====

- Security fix for [GHSA-27r3-jxmh-p828](GHSA-27r3-jxmh-p828) (CVE-2026-111181)
- feat: add `zstd` content-encoding support ([#&#8203;586](expressjs/body-parser#586))
  - Requires Node.js `^22.15.0` or `>=23.8.0`; older versions keep returning `415` for `zstd` encoded bodies
- fix: avoid mutating the parser options and the request object while reading the body ([#&#8203;742](expressjs/body-parser#742), [#&#8203;750](expressjs/body-parser#750))
- perf(json): avoid first-char regex for direct JSON bodies ([#&#8203;744](expressjs/body-parser#744))
- deps:
  - content-type@^2.1.0
  - iconv-lite^0.7.3
  - qs@^6.16.1

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - "before 9am on tuesday"
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xNzUuMSIsInVwZGF0ZWRJblZlciI6IjQ0LjE3NS4xIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiLCJyZW5vdmF0ZSJdfQ==-->

Reviewed-on: https://code.futo.org/harbor/harbor/pulls/2043
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants