Conversation
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
|
I have no opinion on this, but it feels strange, that primitives drop by nearly 50 %. |
Yep, this PR is not good. I want try to fix primitive type |
|
I’ve a bad news. TLTR: the benchmark is not reliable I was trying to understand why primitive types performed worse than objects in my PR, but I realized that on primitive types the code of the eg, see this benchmark case https://github.com/fastify/fast-json-stringify/blob/main/benchmark/bench.js#L44 This schema {
"type": "string"
}generate in both case (master and PR), this code: module.exports = function anonymous(validator, serializer
) {
const {
asString,
asNumber,
asBoolean,
asDateTime,
asDate,
asTime,
asUnsafeString
} = serializer
const asInteger = serializer.asInteger.bind(serializer)
const JSON_STR_BEGIN_OBJECT = '{'
const JSON_STR_END_OBJECT = '}'
const JSON_STR_BEGIN_ARRAY = '['
const JSON_STR_END_ARRAY = ']'
const JSON_STR_COMMA = ','
const JSON_STR_COLONS = ':'
const JSON_STR_QUOTE = '"'
const JSON_STR_EMPTY_OBJECT = JSON_STR_BEGIN_OBJECT + JSON_STR_END_OBJECT
const JSON_STR_EMPTY_ARRAY = JSON_STR_BEGIN_ARRAY + JSON_STR_END_ARRAY
const JSON_STR_EMPTY_STRING = JSON_STR_QUOTE + JSON_STR_QUOTE
const JSON_STR_NULL = 'null'
function main(input) {
let json = ''
if (typeof input !== 'string') {
if (input === null) {
json += JSON_STR_EMPTY_STRING
} else if (input instanceof Date) {
json += JSON_STR_QUOTE + input.toISOString() + JSON_STR_QUOTE
} else if (input instanceof RegExp) {
json += asString(input.source)
} else {
json += asString(input.toString())
}
} else {
json += asString(input)
}
return json
}
return main
}(validator, serializer)The same code had to give the same results... so I decided to benchmark master vs master to see if I got the same results
instead, even if I compared the same branch, I got the result that the second run was better than the first one! We need a more reliable benchmark to accept PR on performance like this one (see #803). |
|
NB: Before accepting, we need a more precise benchmark. see #803 |

It'a a good performance improvments on Array and Object, but not on primitive types
Raw benchmark