-
-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: Improve performance by caching verifier #360
Conversation
@SeanReece ping! do you need any help with this? |
@kibertoad Thanks for reaching out - work has just been busy lately. I've been spending a bit more time getting familiar with the |
function decode (token, options) { | ||
assert(token, 'missing token') | ||
|
||
let selectedDecoder = decoder | ||
|
||
if (options && typeof options !== 'function') { | ||
if (options && options !== decodeOptions && typeof options !== 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
||
if (typeof options === 'function' && !next) { | ||
if (next === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just moving this from below to preserve options passed in even when using promises.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great job!
Description
Closes: #358
This PR fixes an issue causing
fast-jwt
verifier to be recreated on every request, which means thefast-jwt
cache was not being used. By caching thefast-jwt
verifier 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-jwt
cache 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 test
andnpm run benchmark
and the Code of conduct