Skip to content

Instantly share code, notes, and snippets.

@satazor
Forked from mayankchoubey/node_app.mjs
Created May 14, 2024 23:59
Show Gist options
  • Save satazor/1746e7f2ce2f5363b1f21d403b790480 to your computer and use it in GitHub Desktop.
Save satazor/1746e7f2ce2f5363b1f21d403b790480 to your computer and use it in GitHub Desktop.
Node.js - JWT sign & verify
import jwt from "jsonwebtoken";
import { readFileSync } from "node:fs";
import { createSecretKey } from 'node:crypto';
const emails = JSON.parse(
readFileSync(
"/Users/mayankc/Work/source/perfComparisons/testdata/emails.json",
),
);
let i = 1, idx = 0;
const jwtSecret = createSecretKey(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