|
1 | | -const { cpus } = require('os') |
| 1 | +import { cpus } from 'node:os' |
2 | 2 |
|
3 | | -const nodeArgon2 = require('argon2') |
4 | | -const { Suite } = require('benchmark') |
5 | | -const chalk = require('chalk') |
| 3 | +import nodeArgon2 from 'argon2' |
| 4 | +import { Bench } from 'tinybench' |
6 | 5 |
|
7 | | -const { hash, verify, Algorithm } = require('../index') |
| 6 | +import { hash, verify, Algorithm } from '../index.js' |
8 | 7 |
|
9 | 8 | const PASSWORD = '$v=19$m=4096,t=3,p=1$fyLYvmzgpBjDTP6QSypj3g$pb1Q3Urv1amxuFft0rGwKfEuZPhURRDV7TJqcBnwlGo' |
10 | 9 | const CORES = cpus().length |
11 | 10 |
|
12 | | -const suite = new Suite('Hash with all cores') |
13 | | - |
14 | | -suite |
15 | | - .add( |
16 | | - '@node-rs/argon', |
17 | | - async (deferred) => { |
18 | | - await hash(PASSWORD, { |
19 | | - algorithm: Algorithm.Argon2id, |
20 | | - parallelism: CORES, |
21 | | - }) |
22 | | - deferred.resolve() |
23 | | - }, |
24 | | - { defer: true }, |
25 | | - ) |
26 | | - .add( |
27 | | - 'node-argon', |
28 | | - async (deferred) => { |
29 | | - await nodeArgon2.hash(PASSWORD, { type: nodeArgon2.argon2id, parallelism: CORES }) |
30 | | - deferred.resolve() |
31 | | - }, |
32 | | - { |
33 | | - defer: true, |
34 | | - }, |
35 | | - ) |
36 | | - .on('cycle', function (event) { |
37 | | - console.info(String(event.target)) |
| 11 | +const HASHED = await hash(PASSWORD, { |
| 12 | + algorithm: Algorithm.Argon2id, |
| 13 | + parallelism: CORES, |
| 14 | +}) |
| 15 | + |
| 16 | +const bench = new Bench('Hash with all cores') |
| 17 | + |
| 18 | +bench |
| 19 | + .add('@node-rs/argon hash', async () => { |
| 20 | + await hash(PASSWORD, { |
| 21 | + algorithm: Algorithm.Argon2id, |
| 22 | + parallelism: CORES, |
| 23 | + }) |
| 24 | + }) |
| 25 | + .add('node-argon hash', async () => { |
| 26 | + await nodeArgon2.hash(PASSWORD, { type: nodeArgon2.argon2id, parallelism: CORES }) |
38 | 27 | }) |
39 | | - .on('complete', function () { |
40 | | - console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) |
| 28 | + .add('@node-rs/argon verify', async () => { |
| 29 | + console.assert(await verify(HASHED, PASSWORD)) |
41 | 30 | }) |
42 | | - .run() |
| 31 | + .add('node-argon verify', async () => { |
| 32 | + console.assert(await nodeArgon2.verify(HASHED, PASSWORD)) |
| 33 | + }) |
| 34 | + |
| 35 | +await bench.warmup() |
| 36 | +await bench.run() |
| 37 | + |
| 38 | +console.table(bench.table()) |
0 commit comments