What is the fastest node.js hashing algorithm

Chris Thompson
1 min readSep 15, 2017

--

Node.js supports hashing data using three algorithms and two digests.

If you just need a hash for a unique ID, and not cryptography, which one is the fastest?

TLDR;

require('crypto').createHash('sha1').update(data).digest('base64');

Install benchmark.js

mkdir benchmarks
cd benchmarks
npm install benchmark

Create a benchmarking script

vim createHash.jsconst Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
const hash = require('crypto').createHash;
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}`, () =>
hash(alg).update(data).digest(digest)
);
}
suite.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run();

Run the benchmark

node creashHash.jsmd5-hex x 425,196 ops/sec ±1.15% (85 runs sampled)
md5-base64 x 426,419 ops/sec ±2.07% (83 runs sampled)
sha1-hex x 431,241 ops/sec ±2.09% (83 runs sampled)
sha1-base64 x 440,628 ops/sec ±2.64% (82 runs sampled)
sha256-hex x 347,245 ops/sec ±2.53% (84 runs sampled)
sha256-base64 x 346,980 ops/sec ±2.78% (86 runs sampled)
Fastest is sha1-base64

--

--