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

fast-json-stringify slow on STR_ESCAPE regex #1005

Closed
cesco69 opened this issue Mar 8, 2024 · 8 comments
Closed

fast-json-stringify slow on STR_ESCAPE regex #1005

cesco69 opened this issue Mar 8, 2024 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@cesco69
Copy link

cesco69 commented Mar 8, 2024

I have a JSON with a lot of strings that take time during the STR_ESCAPE regex evaluation. Some of these strings I know are safe (no need to escape), is there a way to skip regex evaluation in some way?

@cesco69 cesco69 added the help wanted Extra attention is needed label Mar 8, 2024
@mcollina
Copy link
Member

mcollina commented Mar 8, 2024

Not at this time. However a way to do so could be a great addition to the library.
Maybe using a custom keyword in the schema?

@cesco69
Copy link
Author

cesco69 commented Mar 8, 2024

mhh yep, I think is a big booster...

Maybe the "format" in the JSON Schema?

"name": {
    "type": "string",
    "format": "unescape"
 }

and

asString (str, format) {
    if (typeof str !== 'string') {
      if (str === null) {
        return '""'
      }
      if (str instanceof Date) {
        return '"' + str.toISOString() + '"'
      }
      if (str instanceof RegExp) {
        str = str.source
      } else {
        str = str.toString()
      }
    }

    if( format === "unescape"){
      return '"' + str + '"'
    } else if (str.length < 42) {
      return this.asStringSmall(str)
    } else if (STR_ESCAPE.test(str) === false) {
      return '"' + str + '"'
    } else {
      return JSON.stringify(str)
    }
  }

@Uzlopak
Copy link

Uzlopak commented Mar 8, 2024

unescape has already a specific meaning. Lol, it is even a function in javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape

So unescape is not considerable.
I would prefer something like format "as-is" or "raw" or "pass-through"

@cesco69
Copy link
Author

cesco69 commented Mar 8, 2024

Side note, why asStringSmall is just for string of length 42? and why 42?

@Uzlopak
Copy link

Uzlopak commented Mar 8, 2024

Up to 42. Why 42? Idk

@climba03003
Copy link
Member

I though it is just a random picked number for small string.
42 is the answer to the Ultimate Question of Life, the Universe, and Everything

@kibertoad
Copy link
Member

+1 for passthrough

@cesco69
Copy link
Author

cesco69 commented Mar 8, 2024

I have made a PR fastify/fast-json-stringify#685

benchmark (compare classic bench VS same bench with format raw)

short string............................................. x 22,581,464 ops/sec ±0.79% (186 runs sampled)
short string and raw..................................... x 1,038,659,823 ops/sec ±0.40% (189 runs sampled)

long string without double quotes........................ x 22,477 ops/sec ±0.46% (192 runs sampled)
long string without double quotes raw.................... x 1,035,763,724 ops/sec ±0.39% (189 runs sampled)

long string.............................................. x 15,865 ops/sec ±0.42% (190 runs sampled)
long string raw.......................................... x 1,060,214,143 ops/sec ±0.49% (190 runs sampled)

42 is the answer to the Ultimate Question of Life, the Universe, and Everything

raw string seem x45 faster

@cesco69 cesco69 closed this as completed Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants