perf: cut normalized JSON parsing time by over 60% - #81
Open
RomainLanz wants to merge 1 commit into
Open
Conversation
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.
Hey! 👋🏻
JSON normalization currently runs through a
JSON.parsereviver. Node invokes this callback for every parsed value, even though the bodyparser only needs to transform strings. This makes normalized JSON parsing about three times slower on representative payloads.This change keeps
safeParseand its protections against__proto__andconstructor.prototype, but moves string normalization to an iterative post-parse traversal.The traversal preserves the existing behavior for:
It also avoids recursion during normalization.
Benchmark
Measured on Node.js 26.5.1 with the real
parseJSONfunction and no profiler active. Each variant was warmed up before collecting 25 independent samples. The benchmark alternated the order of the variants between samples.The benchmark includes request stream reading through
parseTextandraw-body, not only the JSON parsing step.Compatibility
prepareJSONParserOptionsnow exposes an internalnormalizerproperty instead ofreviver.This module is not part of the package export map, so public package consumers are unaffected. Applications importing the parser directly from the package sources or replacing
options.revivermanually may need to update their code.Tests
Added coverage for:
All 131 tests, lint, and TypeScript typechecking pass.