Skip to content

Instantly share code, notes, and snippets.

@victor-homyakov
Last active June 3, 2023 15:34
Show Gist options
  • Save victor-homyakov/bcb7d7911e4a388b1c810f8c3ce17bcf to your computer and use it in GitHub Desktop.
Save victor-homyakov/bcb7d7911e4a388b1c810f8c3ce17bcf to your computer and use it in GitHub Desktop.
/*
Setup:
npm i benchmark
npm i string-hash
Results on Node 8.6.0:
md5-hex x 411,933 ops/sec ±0.94% (84 runs sampled)
md5-base64 x 409,007 ops/sec ±2.13% (82 runs sampled)
sha1-hex x 426,509 ops/sec ±2.62% (83 runs sampled)
sha1-base64 x 426,735 ops/sec ±3.52% (80 runs sampled)
sha256-hex x 333,663 ops/sec ±2.73% (84 runs sampled)
sha256-base64 x 333,693 ops/sec ±2.89% (80 runs sampled)
string-hash x 391,622 ops/sec ±0.92% (87 runs sampled)
string-hash2 x 875,667 ops/sec ±0.88% (87 runs sampled)
*/
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
const createHash = require('crypto').createHash;
const hash = require('string-hash');
const data = 'Delightful remarkably mr on announcing themselves entreaties favourable. About to in so terms voice at. Equal an would is found seems of. The particular friendship one sufficient terminated frequently themselves. It more shed went up is roof if loud case. Delay music in lived noise an. Beyond genius really enough passed is up.';
const scenarios = [
{ alg: 'md5', digest: 'hex' },
{ alg: 'md5', digest: 'base64' },
{ alg: 'sha1', digest: 'hex' },
{ alg: 'sha1', digest: 'base64' },
{ alg: 'sha256', digest: 'hex' },
{ alg: 'sha256', digest: 'base64' }
];
for (const { alg, digest } of scenarios) {
suite.add(`${alg}-${digest}`, () =>
createHash(alg).update(data).digest(digest)
);
}
function hash2(str) {
let hash = 5381;
const len = str.length;
for (let i = 0; i < len; i++) {
hash = (hash * 33) ^ str.charCodeAt(i);
}
return hash >>> 0;
}
suite
.add('string-hash', () => hash(data))
.add('string-hash2', () => hash2(data))
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();
@ericcarraway
Copy link

Results on Node 14.15.0 using a 2018 MacBook Pro:

md5-hex       x   423,114 ops/sec  ±2.32% (71 runs sampled)
md5-base64    x   416,342 ops/sec  ±7.12% (71 runs sampled)
sha1-hex      x   427,007 ops/sec  ±6.67% (66 runs sampled)
sha1-base64   x   447,403 ops/sec  ±6.75% (73 runs sampled)
sha256-hex    x   348,171 ops/sec  ±8.85% (74 runs sampled)
sha256-base64 x   344,881 ops/sec ±10.33% (76 runs sampled)
string-hash   x 1,521,080 ops/sec  ±0.11% (94 runs sampled)
string-hash2  x 1,673,553 ops/sec  ±0.26% (93 runs sampled)

Fastest is string-hash2

@pfdgithub
Copy link

MacBook Pro (13-inch, M1, 2020)
Node.js 14.18.1
There is no distinct difference between string-hash and string-hash2

md5-hex x 628,356 ops/sec ±1.51% (84 runs sampled)
md5-base64 x 610,316 ops/sec ±4.21% (81 runs sampled)
sha1-hex x 908,674 ops/sec ±1.52% (72 runs sampled)
sha1-base64 x 905,310 ops/sec ±3.53% (70 runs sampled)
sha256-hex x 875,752 ops/sec ±6.32% (68 runs sampled)
sha256-base64 x 834,902 ops/sec ±7.46% (64 runs sampled)
string-hash x 2,284,139 ops/sec ±0.91% (99 runs sampled)
string-hash2 x 2,317,560 ops/sec ±0.41% (99 runs sampled)
Fastest is string-hash2

@victor-homyakov
Copy link
Author

Fastest is string-hash2

This means that difference is still statistically significant (https://en.wikipedia.org/wiki/Statistical_significance), even if it is very small when measured on this CPU/cache/string size. The difference could be bigger in other circumstances. You can play with the benchmark (run in the latest Node.js version, change string size, add more strings, etc.) to see how it affects the measured difference in speed.

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