|
1 | 1 | const { cpus } = require('os')
|
2 | 2 |
|
3 |
| -const { hashSync, hash, compare } = require('bcrypt') |
4 |
| -const { hashSync: hashSyncJs, hash: hashJs, compare: compareJs } = require('bcryptjs') |
| 3 | +const { hashSync, hash, compare, genSaltSync } = require('bcrypt') |
| 4 | +const { hashSync: hashSyncJs, hash: hashJs, compare: compareJs, genSaltSync: genSaltSyncJs } = require('bcryptjs') |
5 | 5 | const { Suite } = require('benchmark')
|
6 | 6 | const chalk = require('chalk')
|
7 | 7 | const { range } = require('lodash')
|
8 | 8 |
|
9 |
| -const { hash: napiHash, hashSync: napiHashSync, verify } = require('../index') |
| 9 | +const { hash: napiHash, hashSync: napiHashSync, verify, genSaltSync: napiGenSaltSync } = require('../index') |
10 | 10 |
|
11 |
| -const hashRounds = [10, 12, 14] |
12 | 11 | const parallel = cpus().length
|
13 | 12 |
|
14 | 13 | const password = 'node-rust-password'
|
15 | 14 |
|
16 |
| -function runAsync(round) { |
| 15 | +function runAsync(round = 12) { |
17 | 16 | const asyncHashSuite = new Suite(`Async hash round ${round}`)
|
18 | 17 | return new Promise((resolve) => {
|
19 | 18 | asyncHashSuite
|
@@ -53,11 +52,7 @@ function runAsync(round) {
|
53 | 52 | })
|
54 | 53 | }
|
55 | 54 |
|
56 |
| -hashRounds |
57 |
| - .reduce(async (acc, cur) => { |
58 |
| - await acc |
59 |
| - return runAsync(cur) |
60 |
| - }, Promise.resolve()) |
| 55 | +runAsync() |
61 | 56 | .then(
|
62 | 57 | () =>
|
63 | 58 | new Promise((resolve) => {
|
@@ -103,24 +98,47 @@ hashRounds
|
103 | 98 | }),
|
104 | 99 | )
|
105 | 100 | .then(() => {
|
106 |
| - for (const round of hashRounds) { |
107 |
| - const syncHashSuite = new Suite(`Hash round ${round}`) |
| 101 | + return new Promise((resolve) => { |
| 102 | + const syncHashSuite = new Suite(`Hash round 12`) |
108 | 103 | syncHashSuite
|
109 | 104 | .add('@node-rs/bcrypt', () => {
|
110 |
| - napiHashSync(password, round) |
| 105 | + napiHashSync(password, 12) |
111 | 106 | })
|
112 | 107 | .add('node bcrypt', () => {
|
113 |
| - hashSync(password, round) |
| 108 | + hashSync(password, 12) |
114 | 109 | })
|
115 | 110 | .add('bcryptjs', () => {
|
116 |
| - hashSyncJs(password, round) |
| 111 | + hashSyncJs(password, 12) |
117 | 112 | })
|
118 | 113 | .on('cycle', function (event) {
|
119 | 114 | console.info(String(event.target))
|
120 | 115 | })
|
121 | 116 | .on('complete', function () {
|
122 | 117 | console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`)
|
| 118 | + resolve() |
123 | 119 | })
|
124 | 120 | .run()
|
125 |
| - } |
| 121 | + }) |
| 122 | + }) |
| 123 | + .then(() => { |
| 124 | + return new Promise((resolve) => { |
| 125 | + new Suite('genSaltSync') |
| 126 | + .add('@node-rs/bcrypt', () => { |
| 127 | + napiGenSaltSync(10, '2b') |
| 128 | + }) |
| 129 | + .add('node bcrypt', () => { |
| 130 | + genSaltSync(10, 'b') |
| 131 | + }) |
| 132 | + .add('bcryptjs', () => { |
| 133 | + genSaltSyncJs(10) |
| 134 | + }) |
| 135 | + .on('cycle', function (event) { |
| 136 | + console.info(String(event.target)) |
| 137 | + }) |
| 138 | + .on('complete', function () { |
| 139 | + console.info(`${this.name} bench suite: Fastest is ${chalk.green(this.filter('fastest').map('name'))}`) |
| 140 | + resolve() |
| 141 | + }) |
| 142 | + .run() |
| 143 | + }) |
126 | 144 | })
|
0 commit comments