Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Oct 8, 2024

Description

This PR

  • adds support for namedExports with stringify: true
  • adds support for stringify: 'auto'
  • changes the default value of stringify to 'auto' from false

.

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. Also stringify: true did not support namedExports, so it was not possible to enable it by default.

@sapphi-red sapphi-red added p2-nice-to-have Not breaking anything but nice to have (priority) breaking change labels Oct 8, 2024
Copy link

stackblitz bot commented Oct 8, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Comment on lines 53 to 55
// 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)
Copy link
Member Author

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.

Copy link
Member Author

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.

@sapphi-red sapphi-red added this to the 6.0 milestone Oct 16, 2024
Copy link
Member

@ArnaudBarre ArnaudBarre left a 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Status: Approved
Development

Successfully merging this pull request may close these issues.

4 participants