Skip to content

Instantly share code, notes, and snippets.

@moshest
Last active April 12, 2018 09:22
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save moshest/08643030de2febd673040cffea4f05c8 to your computer and use it in GitHub Desktop.
Node.js Performance Comparison Between SHA-512 and SHA-256
# 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 %
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