Last active
February 9, 2021 20:00
-
-
Save maxnachlinger/b21fbb30403e8ffd712e63897a945467 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @typescript-eslint/no-var-requires, no-console, no-undef */ | |
'use strict'; | |
const Benchmark = require('benchmark'); | |
const { createHash } = require('crypto'); | |
const smallInput = '75448dcb-5131-4a45-8ced-01aff65183e8'; | |
const largeInput = smallInput.repeat(75); | |
// eslint-disable-next-line new-cap | |
const suite = Benchmark.Suite({ | |
onError(...args) { | |
console.log('ERROR!', ...args); | |
}, | |
}); | |
const tests = [ | |
{ alg: 'md5', digest: 'hex' }, | |
{ alg: 'md5', digest: 'base64' }, | |
{ alg: 'sha1', digest: 'hex' }, | |
{ alg: 'sha1', digest: 'base64' }, | |
{ alg: 'sha256', digest: 'hex' }, | |
{ alg: 'sha256', digest: 'base64' }, | |
]; | |
[ | |
{ name: 'small input', input: smallInput }, | |
{ name: 'large input', input: largeInput }, | |
].forEach(({ name, input }) => { | |
tests.forEach(({ alg, digest }) => { | |
suite.add(`${alg}-${digest} ${name}`, () => createHash(alg).update(input).digest(digest)); | |
}); | |
}); | |
const results = []; | |
suite | |
.on('cycle', function (event) { | |
results.push(event.target); | |
}) | |
.on('complete', function () { | |
console.log(`### Fastest Hash Benchmarks | |
| name | ops/sec | # of runs | | |
| :---------- | ----------: | ----------: |`); | |
results.forEach((result) => { | |
const { | |
name, | |
hz, | |
stats: { sample }, | |
} = result; | |
const ops = Math.round(hz).toLocaleString('en-US'); | |
const runs = sample.length; | |
console.log(`| ${name} | ${ops} | ${runs} |`); | |
}); | |
}) | |
.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my old macbook pro with Node
14.15.4
:Winners:
sha1-hex
- going to say eithermd5
orsha1
would be fine here :)sha1-base64
Fastest Hash Benchmarks