Created
January 6, 2024 17:44
-
-
Save MaxGraey/36c46d2623a2b885142aef55ea4fc5ff to your computer and use it in GitHub Desktop.
"crypto-js" vs node's crypto bench
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
const crypto = require('node:crypto'); | |
const CryptoJS = require("crypto-js"); | |
const EMAILS = 200_000; | |
const MIN_EMAIL_LEN = 5; | |
const MAX_EMAIL_LEN = 23; | |
const emails = Array.from( | |
{ length: EMAILS }, | |
_ => randomMailGen(randomRange()) | |
); | |
function randomRange(lo = MIN_EMAIL_LEN, hi = MAX_EMAIL_LEN) { | |
const range = hi - lo + 1; | |
return lo + Math.floor(Math.random() * range); | |
} | |
function randomMailGen(len, domain = '@gmail.com') { | |
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789.!#$%&* +-/=?^_`{|}~'; | |
let email = ''; | |
for (let i = 0; i < len; ++i) { | |
const randomIndex = Math.floor(Math.random() * characters.length); | |
email += characters[randomIndex]; | |
} | |
return email + domain; | |
} | |
function bench(name, fn, emails) { | |
const startTime = performance.now(); | |
fn(emails); | |
const delta = performance.now() - startTime; | |
console.log(`${name}: ${delta.toFixed(2)} ms`); | |
} | |
class Hash { | |
static toMd5(email) { | |
return CryptoJS.MD5(email).toString(); | |
} | |
static toSha1(email) { | |
return CryptoJS.SHA1(email).toString(); | |
} | |
} | |
class HashNative { | |
static toMd5(email) { | |
return crypto.createHash('md5') | |
.update(email) | |
.digest('hex'); | |
} | |
static toSha1(email) { | |
return crypto.createHash('sha1') | |
.update(email) | |
.digest('hex'); | |
} | |
} | |
function benchCryptoJS() { | |
emails.map(email => { | |
return { | |
hashMd5: Hash.toMd5(email), | |
hashSha1: Hash.toSha1(email), | |
hashLowerCaseMd5: Hash.toMd5(email.toLowerCase()), | |
hashLowerCaseSha1: Hash.toSha1(email.toLowerCase()), | |
}; | |
}) | |
} | |
function benchCryptoNative() { | |
emails.map(email => { | |
return { | |
hashMd5: HashNative.toMd5(email), | |
hashSha1: HashNative.toSha1(email), | |
hashLowerCaseMd5: HashNative.toMd5(email.toLowerCase()), | |
hashLowerCaseSha1: HashNative.toSha1(email.toLowerCase()), | |
}; | |
}) | |
} | |
function benchCryptoNativeLowercaseOnly() { | |
emails.map(email => { | |
email = email.toLowerCase(); | |
return { | |
canonicalHashMd5: HashNative.toMd5(email), | |
canonicalHashSha1: HashNative.toSha1(email), | |
}; | |
}) | |
} | |
function benchCryptoNativeLowercaseMd5Only() { | |
emails.map(email => { | |
return { | |
hashMd5: HashNative.toMd5(email.toLowerCase()), | |
}; | |
}) | |
} | |
bench('CryptoJs Hashing', benchCryptoJS, emails); | |
bench('NodeJs Hashing', benchCryptoNative, emails); | |
bench('NodeJs Hashing (Lowercase Only)', benchCryptoNativeLowercaseOnly, emails); | |
bench('NodeJs Hashing (Lowercase MD5 Only)', benchCryptoNativeLowercaseMd5Only, emails); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: