Skip to content

Commit

Permalink
Adjust benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jul 1, 2024
1 parent e0ba4c8 commit 4513b87
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions benchmark/hashes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as bench from 'micro-bmark';
const { run, mark } = bench; // or bench.mark
import crypto from 'node:crypto';
import { mark, utils } from 'micro-bmark';
// Noble
import { sha256, sha384, sha512 } from '@noble/hashes/sha2';
// import { sha224, sha512_256, sha512_384 } from '@noble/hashes/sha2';
Expand All @@ -13,6 +11,7 @@ import { ripemd160 } from '@noble/hashes/ripemd160';
import { hmac } from '@noble/hashes/hmac';

// Others
import { createHash as crypto_createHash, createHmac as crypto_createHmac } from 'node:crypto';
import createHash from 'create-hash/browser.js';
import createHmac from 'create-hmac/browser.js';
import stable256 from '@stablelib/sha256';
Expand All @@ -27,35 +26,35 @@ import jssha3 from 'js-sha3';
import nobleUnrolled from 'unrolled-nbl-hashes-sha3';
import { SHA3 as _SHA3 } from 'sha3';
import wasm_ from 'hash-wasm';

const wasm = {};
const wrapBuf = (arrayBuffer) => new Uint8Array(arrayBuffer);

const ONLY_NOBLE = process.argv[2] === 'noble';

const HASHES = {
SHA256: {
node: (buf) => crypto.createHash('sha256').update(buf).digest(),
node: (buf) => crypto_createHash('sha256').update(buf).digest(),
'hash-wasm': (buf) => wasm.sha256.init().update(buf).digest(),
'crypto-browserify': (buf) => createHash('sha256').update(buf).digest(),
stablelib: (buf) => stable256.hash(buf),
'fast-sha256': (buf) => fastsha256.hash(buf),
noble: (buf) => sha256(buf),
},
SHA384: {
node: (buf) => crypto.createHash('sha384').update(buf).digest(),
node: (buf) => crypto_createHash('sha384').update(buf).digest(),
'crypto-browserify': (buf) => createHash('sha384').update(buf).digest(),
stablelib: (buf) => stable2_384.hash(buf),
noble: (buf) => sha384(buf),
},
SHA512: {
node: (buf) => crypto.createHash('sha512').update(buf).digest(),
node: (buf) => crypto_createHash('sha512').update(buf).digest(),
'hash-wasm': (buf) => wasm.sha512.init().update(buf).digest(),
'crypto-browserify': (buf) => createHash('sha512').update(buf).digest(),
stablelib: (buf) => stable2_512.hash(buf),
noble: (buf) => sha512(buf),
},
'SHA3-256, keccak256, shake256': {
node: (buf) => crypto.createHash('sha3-256').update(buf).digest(),
node: (buf) => crypto_createHash('sha3-256').update(buf).digest(),
'hash-wasm': (buf) => wasm.sha3.init().update(buf).digest(),
stablelib: (buf) => new stable3.SHA3256().update(buf).digest(),
'js-sha3': (buf) => wrapBuf(jssha3.sha3_256.create().update(buf).arrayBuffer()),
Expand All @@ -66,13 +65,13 @@ const HASHES = {
Kangaroo12: { noble: (buf) => k12(buf) },
Marsupilami14: { noble: (buf) => m14(buf) },
BLAKE2b: {
node: (buf) => crypto.createHash('blake2b512').update(buf).digest(),
node: (buf) => crypto_createHash('blake2b512').update(buf).digest(),
'hash-wasm': (buf) => wasm.blake2b.init().update(buf).digest(),
stablelib: (buf) => new stableb2b.BLAKE2b().update(buf).digest(),
noble: (buf) => blake2b(buf),
},
BLAKE2s: {
node: (buf) => crypto.createHash('blake2s256').update(buf).digest(),
node: (buf) => crypto_createHash('blake2s256').update(buf).digest(),
'hash-wasm': (buf) => wasm.blake2s.init().update(buf).digest(),
stablelib: (buf) => new stableb2s.BLAKE2s().update(buf).digest(),
noble: (buf) => blake2s(buf),
Expand All @@ -82,12 +81,12 @@ const HASHES = {
noble: (buf) => blake3(buf),
},
RIPEMD160: {
node: (buf) => crypto.createHash('ripemd160').update(buf).digest(),
node: (buf) => crypto_createHash('ripemd160').update(buf).digest(),
'crypto-browserify': (buf) => createHash('ripemd160').update(Buffer.from(buf)).digest(),
noble: (buf) => ripemd160(buf),
},
'HMAC-SHA256': {
node: (buf) => crypto.createHmac('sha256', buf).update(buf).digest(),
node: (buf) => crypto_createHmac('sha256', buf).update(buf).digest(),
'crypto-browserify': (buf) => createHmac('sha256', buf).update(buf).digest(),
stablelib: (buf) => new stableHmac.HMAC(stable256.SHA256, buf).update(buf).digest(),
noble: (buf) => hmac(sha256, buf, buf),
Expand All @@ -104,32 +103,32 @@ const buffers = {
// '1MB': [250, new Uint8Array(1024 * 1024).fill(4)],
};

export const main = () =>
run(async () => {
if (!ONLY_NOBLE) {
wasm.sha256 = await wasm_.createSHA256();
wasm.sha512 = await wasm_.createSHA512();
wasm.sha3 = await wasm_.createSHA3();
wasm.blake2b = await wasm_.createBLAKE2b();
wasm.blake2s = await wasm_.createBLAKE2s();
wasm.blake3 = await wasm_.createBLAKE3();
}
for (let [k, libs] of Object.entries(HASHES)) {
if (!ONLY_NOBLE) console.log(`==== ${k} ====`);
for (const [size, [samples, buf]] of Object.entries(buffers)) {
for (const [lib, fn] of Object.entries(libs)) {
if (ONLY_NOBLE && lib !== 'noble') continue;
// if (lib !== 'noble') continue;
let title = `${k} ${size}`;
if (!ONLY_NOBLE) title += ` ${lib}`;
await mark(title, samples, () => fn(buf));
}
if (!ONLY_NOBLE) console.log();
async function main() {
if (!ONLY_NOBLE) {
wasm.sha256 = await wasm_.createSHA256();
wasm.sha512 = await wasm_.createSHA512();
wasm.sha3 = await wasm_.createSHA3();
wasm.blake2b = await wasm_.createBLAKE2b();
wasm.blake2s = await wasm_.createBLAKE2s();
wasm.blake3 = await wasm_.createBLAKE3();
}
for (let [k, libs] of Object.entries(HASHES)) {
if (!ONLY_NOBLE) console.log(`==== ${k} ====`);
for (const [size, [samples, buf]] of Object.entries(buffers)) {
for (const [lib, fn] of Object.entries(libs)) {
if (ONLY_NOBLE && lib !== 'noble') continue;
// if (lib !== 'noble') continue;
let title = `${k} ${size}`;
if (!ONLY_NOBLE) title += ` ${lib}`;
await mark(title, samples, () => fn(buf));
}
if (!ONLY_NOBLE) console.log();
}
// Log current RAM
bench.utils.logMem();
});
}
// Log current RAM
bench.utils.logMem();
}


// ESM is broken.
import url from 'node:url';
Expand Down

0 comments on commit 4513b87

Please sign in to comment.