Last active
April 12, 2018 09:22
-
-
Save moshest/08643030de2febd673040cffea4f05c8 to your computer and use it in GitHub Desktop.
Node.js Performance Comparison Between SHA-512 and SHA-256
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
# MacBook Pro (Mid 2015) - 2.5 GHz Intel Core i7 | |
# Node.js v9.2.0 | |
block size: 8192 | |
hashing sha512 x 300000 times: 4447ms | |
hashing sha256 x 300000 times: 6105ms | |
difference: 27.158067158067155 % | |
block size: 4096 | |
hashing sha512 x 300000 times: 2601ms | |
hashing sha256 x 300000 times: 3426ms | |
difference: 24.080560420315237 % | |
block size: 1024 | |
hashing sha512 x 300000 times: 1218ms | |
hashing sha256 x 300000 times: 1434ms | |
difference: 15.062761506276146 % | |
block size: 512 | |
hashing sha512 x 300000 times: 970ms | |
hashing sha256 x 300000 times: 1102ms | |
difference: 11.97822141560799 % |
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
const crypto = require('crypto'); | |
measureBlock(8192); | |
measureBlock(4096); | |
measureBlock(1024); | |
measureBlock(512); | |
function measureBlock(blockSize) { | |
const block = crypto.randomBytes(blockSize); | |
console.log('block size:', blockSize); | |
const a = measureHash(block, 'sha512'); | |
const b = measureHash(block, 'sha256'); | |
console.log(` difference: ${100 - 100 * a / b} %`); | |
} | |
function measureHash(block, hashAlgorithm) { | |
const iterations = 30e4; | |
const begin = Date.now(); | |
for (let i = 0; i < iterations; i += 1) { | |
crypto | |
.createHash(hashAlgorithm) | |
.update(block) | |
.digest(); | |
} | |
const end = Date.now(); | |
const diff = end - begin; | |
console.log(` hashing ${hashAlgorithm} x ${iterations} times: ${diff}ms`); | |
return diff; | |
} | |
const crypto = require('crypto'); | |
measureBlock(8192); | |
measureBlock(4096); | |
measureBlock(1024); | |
measureBlock(512); | |
function measureBlock(blockSize) { | |
const block = crypto.randomBytes(blockSize); | |
console.log('block size:', blockSize); | |
const a = measureHash(block, 'sha512'); | |
const b = measureHash(block, 'sha256'); | |
console.log(` difference: ${100 - 100 * a / b} %`); | |
} | |
function measureHash(block, hashAlgorithm) { | |
const iterations = 30e4; | |
const begin = Date.now(); | |
for (let i = 0; i < iterations; i += 1) { | |
crypto | |
.createHash(hashAlgorithm) | |
.update(block) | |
.digest(); | |
} | |
const end = Date.now(); | |
const diff = end - begin; | |
console.log(` hashing ${hashAlgorithm} x ${iterations} times: ${diff}ms`); | |
return diff; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment