Why is crypto.pbkdf2Sync more time-consuming than crypto.pbkdf2?

when you see Synchronize in nodejs debugging Guide , it takes more time than asynchronous one at the CPP level crypto::PBKDF2 .

  1. 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.
  2. 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
    }
  })
}
Mar.19,2021

see comments for positive solutions. Asynchronous versions can use libuv's thread pool.

< del > first of all, your test is wrong. The Synchronize version tests 100 Synchronize hash calls, and the asynchronous version is one of the hash calls. < / del >

console.time('pbkdf2Async');
function test(count) {
  if (count >= 100) {
    console.timeEnd('pbkdf2Async');
    return;
  }
  hash('random_password', (h) => test(count + 1))
}
test(0)

})()

< del > the difference is not big, the number of times increases, Synchronize will even be faster than asynchronous. < / del >

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b39e3b-34598.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b39e3b-34598.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?