Skip to content

Instantly share code, notes, and snippets.

@jimmiehansson
Created July 4, 2019 19:55
Show Gist options
  • Select an option

  • Save jimmiehansson/b5b2d40f87066d45f801b32f6aec1efe to your computer and use it in GitHub Desktop.

Select an option

Save jimmiehansson/b5b2d40f87066d45f801b32f6aec1efe to your computer and use it in GitHub Desktop.
bench hashing algos
const Benchmark = require('benchmark')
const xxhash = require('xxhash')
const siphash = require('siphash')
const fnv1a = require('@sindresorhus/fnv1a')
const { createHash } = require('crypto')
var suite = new Benchmark.Suite
const config_ = {
SECRET: 'This is a secret key',
KEY: ['key1', 'key2', 'key3', 'key4'],
DIGEST: 'This is a digest',
SEED: 0xDEADBEAF,
PAYLOAD: 'This is sample payload'
}
suite.add('crypto::md5', function () {
createHash('md5').update(config_.PAYLOAD).digest()
}).add('crypto::sha-1', function () {
createHash('sha1').update(config_.PAYLOAD).digest()
}).add('xxhash::(rand)1xi32', function () {
xxhash.hash(Buffer.from(config_.PAYLOAD), config_.SEED)
}).add('fnv1a::(rand)i32 -> CRC32', function () {
fnv1a(config_.PAYLOAD)
}).add('siphash::(rand)4xi32 -> 128 output', function () {
siphash.hash_hex(config_.KEY, config_.PAYLOAD)
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
/**
--- oss/fastify-etag ‹feature/hash-algorithms* M?› » node benchmark.js
crypto::md5 x 549,756 ops/sec ±1.01% (84 runs sampled)
crypto::sha-1 x 508,943 ops/sec ±1.78% (76 runs sampled)
xxhash::(rand)1xi32 x 2,512,438 ops/sec ±0.82% (87 runs sampled)
fnv1a::(rand)i32 -> CRC32 x 3,321,856 ops/sec ±1.12% (92 runs sampled)
siphash::(rand)4xi32 -> 128 output x 318,842 ops/sec ±0.64% (88 runs sampled)
Fastest is fnv1a::(rand)i32 -> CRC32
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment