Skip to content

perf: Allow secret and publicKey options to be crypto.KeyObject (2x to 50x faster calls) #1971

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dbaty
Copy link

@dbaty dbaty commented Jun 11, 2025

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Feature
  • Performance

Current and new behaviors

When JwtService is initialized with publicKey as a string or Buffer, verify() and verifyAsync() pass it to "jsonwebtoken.verify()", which creates an instance of crypto.KeyObject from it via crypto.createPublicKey(). This is not free. Initializing publicKey with a KeyObject avoids this transformation in "jsonwebtoken". On my laptop, it makes verify twice faster.

The same goes for secret, used in sign(), verify() and their asynchronous variants. Initializing with a KeyObject (built via crypto.createSecretKey) makes these functions ~50 times faster.

See also auth0/node-jsonwebtoken#966, which reports similar gains.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I tentatively updated the README. Suggestions are most welcome! :) (Note that this README links to jsonwebtoken own README, which has yet to be updated (see aforementioned issue).).

When JwtService is initialized with `publicKey` as a string or Buffer,
`verify()` and `verifyAsync()` pass it to "jsonwebtoken.verify()",
which creates an instance of `crypto.KeyObject` from it via
`crypto.createPublicKey()`. This is not free. Initializing `publicKey`
with a `KeyObject` avoids this transformation in "jsonwebtoken". On my
laptop, it makes `verify` twice faster.

The same goes for `secret`, used in `sign()`, `verify()` and their
asynchronous variants. Initializing with a `KeyObject` (built via
`crypto.createSecretKey`) makes these functions ~50 times faster.

See also auth0/node-jsonwebtoken@966.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant