-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(json)!: add json.stringify: 'auto'
and make that the default
#18303
Conversation
Run & review this pull request in StackBlitz Codeflow. |
// use 10kB as a threshold | ||
// https://v8.dev/blog/cost-of-javascript-2019#:~:text=A%20good%20rule%20of%20thumb%20is%20to%20apply%20this%20technique%20for%20objects%20of%2010%20kB%20or%20larger | ||
(options.stringify === 'auto' && json.length >= 10 * 1000) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should benchmark this to find out whether the threshold has changed over the last five years.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran the benchmark: https://github.com/sapphi-red/json-parse-benchmark
It's difficult to measure where the threshold is due to the overhead of starting the JS engines. But the perf benefit still exists. Probably fine to go with 10kB for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't read all the code but the API looks good to me!
Description
This PR
namedExports
withstringify: true
stringify: 'auto'
stringify
to'auto'
fromfalse
.
It is known that embedding an JSON serializable object as a string reduces the parse cost than embedding the object directly for large objects. This was possible by setting
stringify: true
, but that made all JSON imports into serialized format. Alsostringify: true
did not supportnamedExports
, so it was not possible to enable it by default.