Last active
February 27, 2019 01:43
-
-
Save JustinBeaudry/45b8ad0c1a36eaf0974f8dc1188e2af3 to your computer and use it in GitHub Desktop.
Benchmarking Node Crypto vs XXHash
This file contains hidden or 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
// Benchmarking Node Crypto vs XXHash | |
// | |
// Setup/Installation: | |
// mkdir hashbenchmarks && cd hashbenchmarks && npm i xxhash benchmark beautify-benchmark && curl https://gist.githubusercontent.com/JustinBeaudry/45b8ad0c1a36eaf0974f8dc1188e2af3/raw/202dd48b8a2606ad0be99a8261770ddd4dba74b9/benchmarking-hashes.js -o benchmarking-hashes.js | |
// | |
// Running: | |
// node benchmarking-hashes.js | |
const http = require('http'); | |
const crypto = require('crypto'); | |
const XXHash = require('xxhash'); | |
const Benchmark = require('benchmark'); | |
const benchmarks = require('beautify-benchmark'); | |
const suite = Benchmark.Suite('Crypto vs XXHash', { | |
async: true, | |
onCycle({target}) { | |
benchmarks.add(target); | |
}, | |
onComplete() { | |
benchmarks.log(); | |
} | |
}); | |
const randomStringGeneratorURL = 'http://www.unit-conversion.info/texttools/random-string-generator/?ajax=1'; | |
const getRandomString = (length=100001) => { | |
const formText = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | |
const formOut = 'Z2SfYdZsiNMzKYbP'; | |
const formData = `form%5Btext%5D=${formText}&form%5Bstring_qty%5D=1&form%5Blength%5D=${length}&out=${formOut}`; | |
return new Promise((resolve, reject) => { | |
const request = http.request(randomStringGeneratorURL, { | |
method: 'POST', | |
headers: { | |
Accept: '*/*', | |
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
DNT: 1, | |
Origin: 'https://www.unit-conversion.info', | |
Referrer: 'http://www.unit-conversion.info/texttools/random-string-generator/', | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko)' + | |
' Chrome/72.0.3626.109 Safari/537.36', | |
'X-Requested-With': 'XMLHTTPRequest' | |
} | |
}, response => { | |
if (response.statusCode !== 200) { | |
reject(new Error(`Error fetching random string. code: ${response.statusCode}; message: ${response.statusMessage}`)); | |
return; | |
} | |
response.setEncoding('utf8'); | |
let string = ''; | |
response.on('data', data => { | |
string += data.toString(); | |
}); | |
response.once('end', () => { | |
resolve(string); | |
}); | |
}); | |
request.once('error', err => { | |
reject(err); | |
}); | |
request.write(formData); | |
request.end(); | |
}); | |
}; | |
async function main() { | |
const largeTestData = await getRandomString(); | |
const smallTestData = await getRandomString(100); | |
const largeBufferedTestData = Buffer.from(largeTestData); | |
const smallBufferedTestData = Buffer.from(smallTestData); | |
[ | |
'md4', | |
'md5', | |
'mdc2', | |
'rmd160', | |
'sha1', | |
'sha224', | |
'sha256', | |
'sha384', | |
'sha512' | |
].forEach(algorithm => { | |
[ | |
'ascii', | |
'utf8', | |
'base64', | |
'latin1', | |
'binary', | |
'hex' | |
].forEach(encoding => { | |
suite.add(`crypto-${algorithm}-${encoding} ${largeTestData.length / 1024}MB`, () => { | |
crypto.createHash(algorithm).update(largeTestData).digest(encoding); | |
}); | |
suite.add(`crypto-${algorithm}-${encoding} ${smallTestData.length / 1024}MB`, () => { | |
crypto.createHash(algorithm).update(smallTestData).digest(encoding); | |
}); | |
}); | |
}); | |
[ | |
'buffer', | |
'hex', | |
'base64', | |
'binary' | |
].forEach(encoding => { | |
suite.add(`xxhash-${encoding} ${largeBufferedTestData.byteLength / 1024}MB`, () => { | |
const hash = new XXHash(0xCAFEBABE); | |
hash.update(largeBufferedTestData); | |
hash.digest(encoding); | |
}); | |
suite.add(`xxhash-${encoding} ${smallBufferedTestData.byteLength / 1024}MB`, () => { | |
const hash = new XXHash(0xCAFEBABE); | |
hash.update(smallBufferedTestData); | |
hash.digest(encoding); | |
}); | |
}); | |
suite.run(); | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment