when you see Synchronize in nodejs debugging Guide , it takes more time than asynchronous one at the CPP
level crypto::PBKDF2
.
- doesn"t understand
CPP
, so I can"t look at it from the source code. I want to briefly understand why it caused this result. - whether other methods are similar, such as
readfile/readfilesync
.
Synchronize pbkdf2
const crypto = require("crypto")
function hash (password) {
const salt = crypto.randomBytes(128).toString("base64")
const hash = crypto.pbkdf2Sync(password, salt, 10000, 64, "sha512")
return hash
}
console.time("pbkdf2Sync")
for (let i = 0; i < 100; iPP) {
hash("random_password")
}
console.timeEnd("pbkdf2Sync")//pbkdf2Sync: 917.546ms
Asynchronous pbkdf2
const crypto = require("crypto")
function hash (password, cb) {
const salt = crypto.randomBytes(128).toString("base64")
crypto.pbkdf2(password, salt, 10000, 64, "sha512", cb)
}
let count = 0
console.time("pbkdf2")
for (let i = 0; i < 100; iPP) {
hash("random_password", () => {
countPP
if (count === 100) {
console.timeEnd("pbkdf2")//pbkdf2: 303.675ms
}
})
}