Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Last active November 29, 2016 13:41
Show Gist options
  • Save mafintosh/f0734073fc2d839a98ae29671f26ae07 to your computer and use it in GitHub Desktop.
Save mafintosh/f0734073fc2d839a98ae29671f26ae07 to your computer and use it in GitHub Desktop.
// benchmark hashing 1gb data using sodium-native and node core
var s = require('sodium-native')
var zero = require('dev-zero-stream')
var crypto = require('crypto')
if (process.argv[2] === 'sodium') {
console.log('Benching sodium-native streaming hash')
var then = Date.now()
var stream = s.crypto_generichash_instance()
zero(1024 * 1024 * 1024)
.on('data', function (data) {
stream.update(data)
})
.on('end', function () {
var output = new Buffer(s.crypto_generichash_BYTES)
stream.final(output)
console.log(Date.now() - then, output)
})
} else {
console.log('Benching node core streaming hash')
var then = Date.now()
var stream = crypto.createHash('sha256')
zero(1024 * 1024 * 1024)
.on('data', function (data) {
stream.update(data)
})
.on('end', function () {
var output = stream.digest()
console.log(Date.now() - then, output)
})
}
> node hash-bench.js
Benching node core streaming hash
3289 <Buffer 49 bc 20 df 15 e4 12 a6 44 72 42 1e 13 fe 86 ff 1c 51 65 e1 8b 2a fc cf 16 0d 4d c1 9f e6 8a 14>
> node hash-bench.js sodium
Benching sodium-native streaming hash
1596 <Buffer d5 4d 5b 0e 3d f8 b9 1f e2 f4 86 cc 0b 6f 05 3d 08 c0 a6 ac b5 f6 d9 24 29 5c 06 43 82 77 04 32>
{
"name": "sodium-native-hash-bench",
"dependencies": {
"sodium-native": "*",
"dev-zero-stream": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment