Salt rounds and cost in bcryptjs
The round count sets how much work each hash takes. Raising it makes hashing slower, and the count is stored inside the hash itself.
How it works
- Hash at several costs. The loop hashes the same password with 4, 6, 8, 10 and 12 rounds and measures each call with
performance.now(), so you can see the time grow in your browser. [1] - Read the rounds back.
bcrypt.getRounds(hash)returns the round count stored in each hash, which the example prints next to the timing. [1] - Extract the salt.
bcrypt.getSalt(hash)returns the salt part of the last hash. It only slices the string. [1]
Run and edit it
It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.
import bcrypt from 'bcryptjs'
const password = 's3cret-passphrase'
let lastHash = ''
console.log('rounds | time | getRounds(hash)')
for (const rounds of [4, 6, 8, 10, 12]) {
const t0 = performance.now()
const hash = await bcrypt.hash(password, rounds)
const ms = (performance.now() - t0).toFixed(0)
console.log(`${String(rounds).padStart(6)} | ${ms.padStart(6)} ms | ${bcrypt.getRounds(hash)}`)
lastHash = hash
}
console.log('Last hash: ', lastHash)
console.log('getSalt(hash): ', bcrypt.getSalt(lastHash))
console.log('Still matches? ', await bcrypt.compare(password, lastHash))
Watch the milliseconds grow with each round count; add 13 to the list to see it climb further. Code written by AgentGid [1].
Watch the milliseconds grow with each round count; add 13 to the list to see it climb further. Code written by AgentGid [1].Checked Oct 12, 2026: it starts without errors in Chromium with bcryptjs 3.0.3. Needs WebGL.
More bcryptjs examples
Hashing and checking a password with bcryptjs Synchronous hashing in bcryptjs Checking the 72-byte limit with bcryptjs Hash progress and callbacks in bcryptjs
Sources
Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.