Skip to content

Commit d8485e5

Browse files
committed
Improve bench:compare
1 parent 421645a commit d8485e5

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

test/benchmark/hashes.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import createHmac from 'create-hmac/browser.js';
2323
import { hash as fastsha256 } from 'fast-sha256';
2424
import wasm_ from 'hash-wasm';
2525
import jssha3 from 'js-sha3';
26-
import { createHash as crypto_createHash, createHmac as crypto_createHmac, hkdfSync } from 'node:crypto';
26+
import {
27+
createHash as crypto_createHash,
28+
createHmac as crypto_createHmac,
29+
hkdfSync,
30+
} from 'node:crypto';
2731
import { SHA3 as _SHA3 } from 'sha3';
2832
import nobleUnrolled from 'unrolled-nbl-hashes-sha3';
2933
import { hkdf } from '../../src/hkdf.ts';
@@ -153,13 +157,27 @@ async function main() {
153157
if (buf.length === 8 * 1024) return 6_250;
154158
return 250;
155159
},
160+
metrics: {
161+
'MiB/s': {
162+
rev: true, // Bigger = better (green +%, red -%)
163+
width: 7,
164+
diff: true,
165+
compute: (_obj, stats, perSec, buffer) => {
166+
const MiB = 1024 * 1024;
167+
const ns = 1e9;
168+
const bytesPerOp = 1 * buffer.length;
169+
if (stats.mean === 0n) return 0; // Edge: infinite speed
170+
const speed = (bytesPerOp * ns) / (Number(stats.mean) * MiB);
171+
return +`${speed.toFixed(0)}`;
172+
},
173+
},
174+
},
156175
}
157176
);
158177

159178
await main_hkdf();
160179
}
161180

162-
163181
async function main_hkdf() {
164182
// Usage:
165183
// - basic: node hkdf.js
@@ -180,7 +198,7 @@ async function main_hkdf() {
180198
};
181199
await compare('HKDFs', { length: { 32: 32, 64: 64, 256: 256 } }, HKDF, {
182200
libDims: ['algorithm', 'library'],
183-
defaults: { library: 'noble', buffer: '32B' },
201+
defaults: { library: 'noble', length: '32' },
184202
samples: (length) => {
185203
if (length <= 64) return 100_000;
186204
return 25_000;

0 commit comments

Comments
 (0)