Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Created November 17, 2023 06:29
Show Gist options
  • Save mayankchoubey/881d562f68de254405cebd9fcd3ca170 to your computer and use it in GitHub Desktop.
Save mayankchoubey/881d562f68de254405cebd9fcd3ca170 to your computer and use it in GitHub Desktop.
Node.js - JWT sign & verify
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