Skip to content

Commit 50be77d

Browse files
LinusUBrooooooklyn
andauthored
bench(bcrypt): add @cwasm/openbsd-bcrypt and @cwasm/openwall-bcrypt to benchmark (#649)
* Add @cwasm/openbsd-bcrypt and @cwasm/openwall-bcrypt to benchmark * tinybench --------- Co-authored-by: LongYinan <[email protected]>
1 parent 5232a7e commit 50be77d

File tree

12 files changed

+482
-241
lines changed

12 files changed

+482
-241
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ resolver = "2"
99
blowfish = { version = "0.9", features = ["bcrypt"] }
1010
crc32c = { version = "0.6" }
1111
crc32fast = { version = "1.4", features = ["nightly"] }
12-
getrandom = "0.2"
1312
global_alloc = { path = "./crates/alloc" }
1413
indexmap = { version = "2", features = ["serde"] }
1514
jieba-rs = { version = "0.7", default-features = false, features = ["tfidf", "textrank"] }
@@ -20,6 +19,7 @@ resolver = "2"
2019
napi-derive = { version = "3.0.0-alpha", default-features = false, features = ["type-def"] }
2120
once_cell = "1"
2221
quickcheck = "1.0"
22+
rand = "0.8"
2323
rand_core = { version = "0.6", features = ["getrandom"] }
2424
serde = "1.0"
2525
serde_json = "1.0"

packages/bcrypt/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ crate-type = ["cdylib"]
1111
base64 = { workspace = true }
1212
bcrypt = { workspace = true }
1313
blowfish = { workspace = true }
14-
getrandom = { workspace = true }
1514
global_alloc = { workspace = true }
1615
napi = { workspace = true, default-features = false, features = ["napi3"] }
1716
napi-derive = { workspace = true }
17+
rand = { workspace = true }
1818

1919
[dev-dependencies]
2020
quickcheck = { workspace = true }

packages/bcrypt/README.md

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@
55

66
🚀 Fastest bcrypt in Node.js
77

8-
## Support matrix
9-
10-
| | node12 | node14 | node16 | node18 |
11-
| ---------------- | ------ | ------ | ------ | ------ |
12-
| Windows x64 |||||
13-
| Windows x32 |||||
14-
| Windows arm64 |||||
15-
| macOS x64 |||||
16-
| macOS arm64 |||||
17-
| Linux x64 gnu |||||
18-
| Linux x64 musl |||||
19-
| Linux arm gnu |||||
20-
| Linux arm64 gnu |||||
21-
| Linux arm64 musl |||||
22-
| Android arm64 |||||
23-
| Android armv7 |||||
24-
| FreeBSD x64 |||||
25-
268
## Usage
279

2810
```typescript
@@ -55,33 +37,54 @@ export function genSalt(round: number, version?: Version): Promise<string>
5537
## Bench
5638

5739
```
58-
Model Name: MacBook Pro
59-
Model Identifier: MacBookPro15,1
60-
Processor Name: Intel Core i7
61-
Processor Speed: 2.6 GHz
62-
Number of Processors: 1
63-
Total Number of Cores: 6
64-
L2 Cache (per Core): 256 KB
65-
L3 Cache: 12 MB
66-
Hyper-Threading Technology: Enabled
67-
Memory: 16 GB
40+
,MMMM. Host - xxxxxxxxxxxxxxxxxxxxxxx
41+
.MMMMMM Machine - Mac15,9
42+
MMMMM, Kernel - 24.0.0
43+
.;MMMMM:' MMMMMMMMMM;. OS - macOS 15.0.1 Sequoia
44+
MMMMMMMMMMMMNWMMMMMMMMMMM: DE - Aqua
45+
.MMMMMMMMMMMMMMMMMMMMMMMMWM. WM - Quartz Compositor
46+
MMMMMMMMMMMMMMMMMMMMMMMMM. Packages - 194 (Homebrew), 32 (cargo)
47+
;MMMMMMMMMMMMMMMMMMMMMMMM: Shell - zsh
48+
:MMMMMMMMMMMMMMMMMMMMMMMM: Terminal - warpterminal (Version v0.2024.10.23.14.49.stable_00)
49+
.MMMMMMMMMMMMMMMMMMMMMMMMM. Resolution - 5120x2880@160fps (as 2560x1440)
50+
MMMMMMMMMMMMMMMMMMMMMMMMMMM. 2992x1934@120fps (as 1496x967)
51+
.MMMMMMMMMMMMMMMMMMMMMMMMMM. 2232x1512@60fps (as 1116x756)
52+
MMMMMMMMMMMMMMMMMMMMMMMM Uptime - 1d 2h 32m
53+
;MMMMMMMMMMMMMMMMMMMM. CPU - Apple M3 Max (16)
54+
.MMMM,. .MMMM,. CPU Load - 16%
55+
Memory - 50.1 GB / 134.2 GB
56+
Battery - 78% & Discharging
57+
Disk Space - 624.0 GB / 994.7 GB
6858
```
6959
7060
```text
71-
@node-rs/bcrypt x 18.55 ops/sec ±1.51% (12 runs sampled)
72-
node bcrypt x 16.37 ops/sec ±2.94% (11 runs sampled)
73-
bcryptjs x 3.61 ops/sec ±4.86% (6 runs sampled)
74-
Async hash round 12 bench suite: Fastest is @node-rs/bcrypt
75-
@node-rs/bcrypt x 18.51 ops/sec ±1.60% (12 runs sampled)
76-
node bcrypt x 16.51 ops/sec ±1.51% (11 runs sampled)
77-
bcryptjs x 3.71 ops/sec ±2.23% (6 runs sampled)
78-
Async verify bench suite: Fastest is @node-rs/bcrypt
79-
@node-rs/bcrypt x 4.68 ops/sec ±4.69% (16 runs sampled)
80-
node bcrypt x 3.94 ops/sec ±6.56% (14 runs sampled)
81-
bcryptjs x 3.56 ops/sec ±2.04% (13 runs sampled)
82-
Hash round 12 bench suite: Fastest is @node-rs/bcrypt
83-
@node-rs/bcrypt x 521,917 ops/sec ±2.27% (82 runs sampled)
84-
node bcrypt x 252,333 ops/sec ±2.05% (82 runs sampled)
85-
bcryptjs x 110,578 ops/sec ±2.37% (82 runs sampled)
86-
genSaltSync bench suite: Fastest is @node-rs/bcrypt
61+
❯ yarn workspace @node-rs/bcrypt bench
62+
Hash benchmark
63+
┌─────────┬───────────────────┬─────────┬───────────────────┬──────────┬─────────┐
64+
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
65+
├─────────┼───────────────────┼─────────┼───────────────────┼──────────┼─────────┤
66+
│ 0 │ '@node-rs/bcrypt' │ '20' │ 49142200.63636367 │ '±1.08%' │ 11 │
67+
│ 1 │ 'node bcrypt' │ '20' │ 49259219.81818187 │ '±1.35%' │ 11 │
68+
│ 2 │ 'bcryptjs' │ '17' │ 58142116.79999998 │ '±0.36%' │ 10 │
69+
│ 3 │ 'wasm OpenBSD' │ '17' │ 58318899.99999994 │ '±0.23%' │ 10 │
70+
│ 4 │ 'wasm Openwall' │ '18' │ 53324629.20000016 │ '±0.37%' │ 10 │
71+
└─────────┴───────────────────┴─────────┴───────────────────┴──────────┴─────────┘
72+
Verify benchmark
73+
┌─────────┬───────────────────┬─────────┬────────────────────┬──────────┬─────────┐
74+
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
75+
├─────────┼───────────────────┼─────────┼────────────────────┼──────────┼─────────┤
76+
│ 0 │ '@node-rs/bcrypt' │ '5' │ 192946879.09999993 │ '±0.52%' │ 10 │
77+
│ 1 │ 'node bcrypt' │ '5' │ 199806404.2 │ '±0.36%' │ 10 │
78+
│ 2 │ 'bcryptjs' │ '4' │ 231329516.79999986 │ '±0.13%' │ 10 │
79+
└─────────┴───────────────────┴─────────┴────────────────────┴──────────┴─────────┘
80+
GenSalt benchmark
81+
┌─────────┬───────────────────┬─────────────┬────────────────────┬──────────┬─────────┐
82+
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
83+
├─────────┼───────────────────┼─────────────┼────────────────────┼──────────┼─────────┤
84+
│ 0 │ '@node-rs/bcrypt' │ '4,421,897' │ 226.14727567195578 │ '±0.08%' │ 2210949 │
85+
│ 1 │ 'node bcrypt' │ '4,477,408' │ 223.34345972377827 │ '±0.08%' │ 2238705 │
86+
│ 2 │ 'bcryptjs' │ '821,556' │ 1217.2015511950851 │ '±0.39%' │ 410779 │
87+
│ 3 │ 'wasm OpenBSD' │ '3,685,603' │ 271.3259889021118 │ '±0.23%' │ 1842802 │
88+
│ 4 │ 'wasm Openwall' │ '1,462,251' │ 683.8769076754866 │ '±2.50%' │ 731126 │
89+
└─────────┴───────────────────┴─────────────┴────────────────────┴──────────┴─────────┘
8790
```

packages/bcrypt/benchmark/bcrypt.js

Lines changed: 0 additions & 144 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import openbsd from '@cwasm/openbsd-bcrypt'
2+
import openwall from '@cwasm/openwall-bcrypt'
3+
import { hashSync, compare, genSaltSync } from 'bcrypt'
4+
import bcryptjs from 'bcryptjs'
5+
import { Bench } from 'tinybench'
6+
import chalk from 'chalk'
7+
8+
import { hashSync as napiHashSync, verifySync, genSaltSync as napiGenSaltSync } from '../binding.js'
9+
10+
const password = 'node-rust-password'
11+
12+
const syncHashSuite = new Bench()
13+
syncHashSuite
14+
.add('@node-rs/bcrypt', () => {
15+
napiHashSync(password, 10)
16+
})
17+
.add('node bcrypt', () => {
18+
hashSync(password, 10)
19+
})
20+
.add('bcryptjs', () => {
21+
bcryptjs.hashSync(password, 10)
22+
})
23+
.add('wasm OpenBSD', () => {
24+
openbsd.hashSync(password, 10)
25+
})
26+
.add('wasm Openwall', () => {
27+
openwall.hashSync(password, 10)
28+
})
29+
30+
await syncHashSuite.warmup()
31+
await syncHashSuite.run()
32+
33+
console.info(chalk.green('Hash benchmark'))
34+
console.table(syncHashSuite.table())
35+
36+
const verifySuite = new Bench()
37+
const hashed = napiHashSync(password, 12)
38+
verifySuite
39+
.add('@node-rs/bcrypt', () => {
40+
verifySync(password, hashed)
41+
})
42+
.add('node bcrypt', () => {
43+
compare(password, hashSync(password, 12))
44+
})
45+
.add('bcryptjs', () => {
46+
bcryptjs.compareSync(password, hashed)
47+
})
48+
49+
await verifySuite.warmup()
50+
await verifySuite.run()
51+
52+
console.info(chalk.green('Verify benchmark'))
53+
console.table(verifySuite.table())
54+
55+
const genSaltSuite = new Bench()
56+
genSaltSuite
57+
.add('@node-rs/bcrypt', () => {
58+
napiGenSaltSync(12)
59+
})
60+
.add('node bcrypt', () => {
61+
genSaltSync(12)
62+
})
63+
.add('bcryptjs', () => {
64+
bcryptjs.genSaltSync(12)
65+
})
66+
.add('wasm OpenBSD', () => {
67+
openbsd.genSaltSync(12)
68+
})
69+
.add('wasm Openwall', () => {
70+
openwall.genSaltSync(12)
71+
})
72+
73+
await genSaltSuite.warmup()
74+
await genSaltSuite.run()
75+
76+
console.info(chalk.green('GenSalt benchmark'))
77+
console.table(genSaltSuite.table())
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

packages/bcrypt/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
},
6161
"scripts": {
6262
"artifacts": "napi artifacts -d ../../artifacts",
63-
"bench": "cross-env NODE_ENV=production node benchmark/bcrypt.js",
63+
"bench": "cross-env NODE_ENV=production node --import @swc-node/register/esm-register benchmark/bcrypt.ts",
6464
"build": "napi build --platform --release --js binding.js --dts binding.d.ts",
6565
"build:debug": "napi build --platform",
6666
"prepublishOnly": "napi prepublish",
@@ -70,11 +70,15 @@
7070
"url": "https://github.com/napi-rs/node-rs/issues"
7171
},
7272
"devDependencies": {
73+
"@cwasm/openbsd-bcrypt": "^0.1.0",
74+
"@cwasm/openwall-bcrypt": "^0.1.0",
7375
"@napi-rs/cli": "^3.0.0-alpha.63",
7476
"@types/bcrypt": "^5.0.2",
77+
"@types/bcryptjs": "^2.4.6",
7578
"bcrypt": "^5.1.1",
7679
"bcryptjs": "^2.4.3",
77-
"cross-env": "^7.0.3"
80+
"cross-env": "^7.0.3",
81+
"tinybench": "^2.9.0"
7882
},
7983
"funding": {
8084
"type": "github",

0 commit comments

Comments
 (0)