perf: cache the creation of the algorithm #47
Closed
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.
By submitting a PR to this repository, you agree to the terms within the Auth0 Code of Conduct. Please see the contributing guidelines for how to create and submit a high-quality PR for this repo.
Description
This library is used by node-jws, every time someone wants to validate a token, they call
jwa
to create the verify function and then discard the object.Because of this, I read the code of this library and I found that the object with
sign
andverify
function is created every call and also runs aregex
to get the algorithm and the bits.The current performance is:
So, instead of creating it every time, I cache the objects with
Object.freeze
to prevent modification, and also use a object with the key being the hashes and the value being the cached objects, now the performance is:Is an increase in the performance of almost 10x for all cases, also, we reduce to zero the garbage collection by reusing instead of allocating.
More about memory usage
Before:
Now:
The RS256 with 1B op/s probably is caused by optimization of v8 bail-out after discovering the function could receive values other than RS256.
Testing
I didn't change the behavior, I only introduce a cache and freeze of the objects.
Object.freeze
was introduced on NodeJS v0.10.0, so I don't think we will have some compatibility issues.Checklist