Created
November 17, 2023 06:29
-
-
Save mayankchoubey/881d562f68de254405cebd9fcd3ca170 to your computer and use it in GitHub Desktop.
Node.js - JWT sign & verify
This file contains 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
import jwt from "jsonwebtoken"; | |
import { readFileSync } from "node:fs"; | |
const emails = JSON.parse( | |
readFileSync( | |
"/Users/mayankc/Work/source/perfComparisons/testdata/emails.json", | |
), | |
); | |
let i = 1, idx = 0; | |
const jwtSecret = process.env.JWT_SECRET; | |
const numIterations = parseInt(process.argv[2]); | |
let startTS; | |
while (true) { | |
if (i === 10000) { | |
startTS = Date.now(); | |
} | |
const email = emails[idx]; | |
const currTS = Date.now(); | |
const token = jwt.sign({ | |
sub: email, | |
iat: currTS, | |
exp: currTS + 2 * 60 * 60 * 1000, | |
}, jwtSecret); | |
const claims = jwt.verify(token, jwtSecret); | |
if (claims.sub !== email) { | |
process.exit(1); | |
} | |
if (idx++ >= emails.length) { | |
idx = 0; | |
} | |
if (i++ > numIterations) { | |
break; | |
} | |
} | |
const endTS = Date.now(); | |
const diff = endTS - startTS; | |
console.log(diff); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment