fast-json-stringify slow on STR_ESCAPE regex
cesco69 opened this issue · 8 comments
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?
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?
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)
}
}
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"
Side note, why asStringSmall
is just for string of length 42? and why 42?
Up to 42. Why 42? Idk
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
+1 for passthrough
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