-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.mjs
68 lines (62 loc) · 1.53 KB
/
sign.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { JwtClient } from '../index.js';
import bench from 'benchmark';
import chalk from 'chalk';
import fastJwt from 'fast-jwt';
import * as jose from 'jose';
import jwt from 'jsonwebtoken';
const suite = new bench.Suite('Sign token');
const secret = 'somelongsecretasdbnakwfbjawf';
const minSamples = 100;
const encodedKey = new TextEncoder().encode(secret);
const payload = { userId: 'abc123' };
const client = new JwtClient(secret);
const joseSign = async (payload) => {
const s = new jose.SignJWT(payload);
return s.setProtectedHeader({ alg: 'HS256' }).sign(encodedKey);
// const key = jose.JWK.asKey(secret);
// return await jose.JWT.sign(payload, key);
};
suite
.add(
'jsonwebtoken',
() => {
jwt.sign(payload, secret);
},
{ minSamples },
)
.add(
'jose',
async (deferred) => {
await joseSign(payload);
deferred.resolve();
// const s = new jose.SignJWT(payload);
// s.setProtectedHeader({ alg: "HS256" }).sign(encodedKey).then(defer);
},
{ defer: true, minSamples },
)
.add(
'fast-jwt',
() => {
const signer = fastJwt.createSigner({ key: secret });
signer(payload);
},
{ minSamples },
)
.add(
'@carbonteq/jwt',
() => {
client.sign(payload, 1000);
},
{ minSamples },
)
.on('cycle', (e) => {
console.log(String(e.target));
})
.on('complete', function () {
console.log(
`\nSUITE <${this.name}>: Fastest is ${chalk.green(
this.filter('fastest').map('name'),
)}`,
);
})
.run();