Skip to content

Instantly share code, notes, and snippets.

@maxnachlinger
Last active February 9, 2021 20:00
Show Gist options
  • Save maxnachlinger/b21fbb30403e8ffd712e63897a945467 to your computer and use it in GitHub Desktop.
Save maxnachlinger/b21fbb30403e8ffd712e63897a945467 to your computer and use it in GitHub Desktop.
/* 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();
@maxnachlinger
Copy link
Author

maxnachlinger commented Feb 5, 2021

On my old macbook pro with Node 14.15.4:

Winners:

  • Small input: sha1-hex - going to say either md5 or sha1 would be fine here :)
  • Large input: sha1-base64

Fastest Hash Benchmarks

name ops/sec # of runs
md5-hex small input 513,351 62
md5-base64 small input 519,006 63
sha1-hex small input 522,188 63
sha1-base64 small input 516,202 62
sha256-hex small input 475,801 63
sha256-base64 small input 485,721 64
md5-hex large input 180,864 79
md5-base64 large input 192,013 79
sha1-hex large input 213,275 75
sha1-base64 large input 230,429 79
sha256-hex large input 128,407 80
sha256-base64 large input 133,239 83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment