perf: Improve performance by caching verifier#360
Merged
kibertoad merged 2 commits intofastify:masterfrom Jan 22, 2025
Merged
Conversation
mcollina
reviewed
Jan 9, 2025
Member
|
@SeanReece ping! do you need any help with this? |
Contributor
Author
|
@kibertoad Thanks for reaching out - work has just been busy lately. I've been spending a bit more time getting familiar with the |
SeanReece
commented
Jan 21, 2025
| let selectedDecoder = decoder | ||
|
|
||
| if (options && typeof options !== 'function') { | ||
| if (options && options !== decodeOptions && typeof options !== 'function') { |
Contributor
Author
There was a problem hiding this comment.
a new decoder was also being created on every request (see line 478), this ensures we will use the global decoder if the default/global settings are used.
SeanReece
commented
Jan 21, 2025
| const request = this | ||
|
|
||
| if (typeof options === 'function' && !next) { | ||
| if (next === undefined) { |
Contributor
Author
There was a problem hiding this comment.
Just moving this from below to preserve options passed in even when using promises.
gurgunday
approved these changes
Jan 22, 2025
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.
Description
Closes: #358
This PR fixes an issue causing
fast-jwtverifier to be recreated on every request, which means thefast-jwtcache was not being used. By caching thefast-jwtverifier based on the options passed in, an existing verifier instance can be reused to verify incoming request JWTs instead of having to recreate a new verifier, and then thefast-jwtcache can be utilized (if enabled).Benchmarks
Tested using a simple fastify server with autocannon used to create load
No auth: 77,978 req/s
Changes (no cache): 53,008 req/s
Changes (cache): 60,880 req/s
Main (no cache): 46,302 req/s
Main (cache): 34,669.1 req/s
Config used:
Checklist
npm run testandnpm run benchmarkand the Code of conduct