-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathbench.js
59 lines (52 loc) · 1.44 KB
/
bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import bcrypt from "bcrypt";
import bcryptjs from "../index.js";
const pass = "ä☺𠜎️☁";
const testRounds = [8, 9, 10, 11, 12, 13, 14, 15];
function testSync(name, salt, impl) {
var res;
console.time(name);
res = impl.hashSync(pass, salt);
console.timeEnd(name);
console.log("`" + res + "` ");
}
function testAsync(name, salt, impl, cb) {
console.time(name);
impl.hash(pass, salt, function (err, res) {
console.timeEnd(name);
console.log("`" + res + "` ");
if (cb) cb();
});
}
function testMax(name, impl) {
var s = "",
salt = bcryptjs.genSaltSync(4),
last = null;
while (s.length < 100) {
s += "0";
var hash = impl.hashSync(s, salt);
if (hash === last) {
console.log(name + " maximum input length is: " + (s.length - 1));
break;
}
last = hash;
}
}
testMax("bcrypt.js", bcryptjs);
console.log("## Comparing bcryptjs with bcrypt\n");
function next() {
if (testRounds.length === 0) return;
(function (rounds) {
var salt = bcryptjs.genSaltSync(rounds);
console.log("#### Using " + rounds + " rounds");
console.log("Salt: `" + salt + "` ");
testSync("* **bcrypt** sync", salt, bcrypt);
testSync("* **bcrypt.js** sync", salt, bcryptjs);
testAsync("* **bcrypt** async", salt, bcrypt, function () {
testAsync("* **bcrypt.js** async", salt, bcryptjs, function () {
console.log("");
next();
});
});
})(testRounds.shift());
}
next();