Improve object keys performance - #3130
Open
joshkel wants to merge 1 commit into
Open
Conversation
I noticed that constructing Joi.object schemas of complex object types was causing significant slowdown in my Node.js application startup. Profiling revealed that a major source of slowdown was the topographical sorting within object keys' `rebuild`: `sort` was called for each added property, resulting in N * N topographical sorts. To address this, this PR uses @hapi/topo's `manual` option to sort once, at the end. If that sort fails, it redoes the rebuild operation, sorting per key as the current behavior, so that `tryWithPath` can throw the more detailed error message. The updated code constructs my application's schemas roughly 3.6x faster. The preexisting test suite did not cover `rebuild` / @hapi/topo's error handling, so I added tests to cover the affected behavior.
Marsup
reviewed
Jul 30, 2026
| const topo = new Topo.Sorter(); | ||
| for (const child of schema.$_terms.keys) { | ||
| Common.tryWithPath(() => topo.add(child, { after: child.schema.$_rootReferences(), group: child.key }), child.key); | ||
| Common.tryWithPath(() => topo.add(child, { after: child.schema.$_rootReferences(), group: child.key, manual: true }), child.key); |
Collaborator
There was a problem hiding this comment.
Very nice find! Though I'd go for a less repetitive version like calling an external function:
schema.$_terms.keys = new internals.Keys(...internals.sortKeys(schema.$_terms.keys));
.....
internals.sortKeys = function (keys, manual = true) {
const topo = new Topo.Sorter();
for (const child of keys) {
Common.tryWithPath(() => topo.add(child, { after: child.schema.$_rootReferences(), group: child.key, manual }), child.key);
}
try {
return topo.sort();
}
catch {
return internals.sortKeys(keys, false);
}
};
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.
I noticed that constructing Joi.object schemas of complex object types was causing significant slowdown in my Node.js application startup. Profiling revealed that a major source of slowdown was the topographical sorting within object keys'
rebuild:sortwas called for each added property, resulting in N * N topographical sorts.To address this, this PR uses @hapi/topo's
manualoption to sort once, at the end. If that sort fails, it redoes the rebuild operation, sorting per key as the current behavior, so thattryWithPathcan throw the more detailed error message.The updated code constructs my application's schemas roughly 3.6x faster.
The preexisting test suite did not cover
rebuild/ @hapi/topo's error handling, so I added tests to cover the affected behavior.