Last active
June 3, 2023 15:34
-
-
Save victor-homyakov/bcb7d7911e4a388b1c810f8c3ce17bcf to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
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(); |
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
MacBook Pro (13-inch, M1, 2020)
Node.js 14.18.1
There is no distinct difference between string-hash and string-hash2