Skip to content

Conversation

dbaty
Copy link
Contributor

@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.
signOptions?: jwt.SignOptions;
secret?: string | Buffer;
publicKey?: string | Buffer;
secret?: jwt.Secret;
Copy link
Member

@micalevisk micalevisk Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure that we can still assign string or Buffer to these jwt.Secret fields? I didn't check the types nor the test suite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can use string or Buffer. See README of jsonwebtoken and code (here for example).

Tests in @nestjs/jwt already made sure that we can use a Buffer. No (non-mocked) tests used strings. I did not change that part.

@kamilmysliwiec kamilmysliwiec added the enhancement New feature or request label Oct 10, 2025
@kamilmysliwiec kamilmysliwiec merged commit 566bb5b into nestjs:master Oct 10, 2025
1 check passed
@MohammadiSina
Copy link

Nice catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants