Performance: Password hashing with bcrypt
This affects server response time during user authentication and registration due to CPU-intensive hashing.
const bcrypt = require('bcrypt'); async function hashPassword(password) { const saltRounds = 12; // recommended secure level return await bcrypt.hash(password, saltRounds); }
const bcrypt = require('bcrypt'); async function hashPassword(password) { const saltRounds = 4; // very low return await bcrypt.hash(password, saltRounds); }
| Pattern | CPU Load | Event Loop Blocking | Response Delay | Verdict |
|---|---|---|---|---|
| Low salt rounds (e.g., 4) | Low CPU | No blocking | Fast response | [!] OK but insecure |
| Recommended salt rounds (e.g., 12) | High CPU | No blocking if async | Moderate delay (~100ms) | [OK] Secure and performant |
| Synchronous bcrypt calls | High CPU | Blocks event loop | Delays all requests | [X] Bad for server responsiveness |
bcrypt in a Node.js application?await and two arguments: password and salt rounds.const bcrypt = require('bcrypt');
async function check() {
const password = 'secret123';
const hash = await bcrypt.hash(password, 10);
const result = await bcrypt.compare('secret123', hash);
console.log(result);
}
check();const bcrypt = require('bcrypt');
async function verify() {
const password = 'mypassword';
const hash = bcrypt.hash(password, 10);
const match = await bcrypt.compare(password, hash);
console.log(match);
}
verify();bcrypt.hash with salt rounds (12) and async bcrypt.compare is best practice.Pass the test and this pattern moves unknown