perf(serializer): remove all anonymous functions (huge perf improvements!)#812
Merged
mcollina merged 1 commit intofastify:mainfrom Jan 15, 2026
Merged
perf(serializer): remove all anonymous functions (huge perf improvements!)#812mcollina merged 1 commit intofastify:mainfrom
mcollina merged 1 commit intofastify:mainfrom
Conversation
Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
Contributor
Author
|
If this PR gets approved I would like to move the methods function inlineAsInteger (options, input) {
let roundingFn = 'Math.trunc'
if (options && options.rounding) {
switch (options.rounding) {
case 'floor':
roundingFn = 'Math.floor'
break
case 'ceil':
roundingFn = 'Math.ceil'
break
case 'round':
roundingFn = 'Math.round'
break
}
}
return `
if (Number.isInteger(${input})) {
json += ${input}
} else if (typeof ${input} === 'bigint') {
json += ${input}.toString()
} else {
const integer = ${roundingFn}(${input})
if (integer === Infinity || integer === -Infinity || integer !== integer) {
throw new Error('The value "' + ${input} + '" cannot be converted to an integer.')
}
json += integer
}
`
} |
Closed
climba03003
approved these changes
Jan 15, 2026
Member
|
Wow, good work! |
Member
|
Should we update the readme bench too? |
Contributor
Author
Also see #816 for better output format! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Inline generated serializers to remove anonymous functions
This PR removes the generation of per-schema anonymous serializer functions and fully inlines all serialization logic into a single main function.
Eg. this schema:
Actually generate this output code:
Show
const { asString, asNumber, asBoolean, asDateTime, asDate, asTime, asUnsafeString } = serializer
const asInteger = serializer.asInteger.bind(serializer)
let addComma = false
}
let addComma = false
With this PR the output become
Show
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 = ''
const obj_0 = (input && typeof input.toJSON === 'function')
? input.toJSON()
: input
if (obj_0 === null) {
json += JSON_STR_EMPTY_OBJECT
} else {
json += JSON_STR_BEGIN_OBJECT
let addComma_1 = false
}
return json
}
return main
This avoids:
Better JIT optimization opportunities
Inlining allows the JS engine to:
With separate functions, V8 often cannot inline aggressively due to:
Reduced memory pressure:
Benchmark