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: unsafe string format #686

Merged
merged 1 commit into from
Mar 10, 2024
Merged

feat: unsafe string format #686

merged 1 commit into from
Mar 10, 2024

Conversation

nigrosimone
Copy link
Contributor

This PR add the 'unsafe' string format. By default, fast-json-stringify escapes all strings. With the 'unsafe' format, the string isn't escaped. This has a potentially dangerous security issue. You can use it only if you are sure that your data doesn't need escaping. The advantage is a significant performance improvement.

Usage into the JSON schema

const stringify = fastJson({
  title: 'Example Schema',
  type: 'object',
  properties: {
    'code': {
      type: 'string',
      format 'unsafe'
    }
  }
})

benchmark (safe VS unsafe):

short string............................................. x 14,605,189 ops/sec ±3.85% (157 runs sampled)
unsafe short string...................................... x 693,266,405 ops/sec ±3.05% (187 runs sampled)

long string without double quotes........................ x 19,598 ops/sec ±3.48% (165 runs sampled)
unsafe long string without double quotes................. x 378,676,248 ops/sec ±2.95% (157 runs sampled)

long string.............................................. x 6,330 ops/sec ±5.32% (169 runs sampled)
unsafe long string....................................... x 555,420,885 ops/sec ±2.04% (167 runs sampled)

side note: this PR is the continuation of the #685 (cesco69 is my other account). #685 will be closed and I'll continue here

Checklist

@gurgunday
Copy link
Member

Did you know about #685

@nigrosimone
Copy link
Contributor Author

@gurgunday

Did you know about #685

Yes. Read my PR side note:

side note: this PR is the continuation of the #685 (cesco69 is my other account). #685 will be closed and I'll continue here

@gurgunday
Copy link
Member

Sorry saw on my phone, I see

Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@ivan-tymoshenko ivan-tymoshenko left a comment

Choose a reason for hiding this comment

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

lgtm. Nice job 👍

Comment on lines +643 to +646
'code': {
type: 'string',
format 'unsafe'
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'code': {
type: 'string',
format 'unsafe'
}
'code': {
type: 'string',
format 'unsafe'
}

@ivan-tymoshenko ivan-tymoshenko merged commit 1197be3 into fastify:master Mar 10, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants