Skip to content

Commit 16eae15

Browse files
committed
fix benchmarks: replace nanobench with tinybench
1 parent b4f43ea commit 16eae15

File tree

7 files changed

+96
-37
lines changed

7 files changed

+96
-37
lines changed

benchmark/all.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import './bencode.js'
2+
import './buffer-vs-string.js'
3+
import './compare-decode.js'
4+
import './compare-encode.js'
5+
import './encoding-length.js'

benchmark/bencode.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import bench from 'nanobench'
3+
import { fileURLToPath } from 'url'
4+
import { Bench } from 'tinybench'
45

56
import bencode from '../index.js'
67

8+
const __filename = fileURLToPath(import.meta.url)
9+
const __dirname = path.dirname(__filename)
10+
711
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
812
const object = bencode.decode(buffer)
913
const objectUtf8 = bencode.decode(buffer, 'utf8')
@@ -12,7 +16,9 @@ const objectBinary = bencode.decode(buffer, 'binary')
1216

1317
const ITERATIONS = 10000
1418

15-
bench(`bencode.encode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
19+
const bench = new Bench({ time: 100 })
20+
21+
bench.add(`bencode.encode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
1622
let result = null
1723

1824
run.start()
@@ -24,7 +30,7 @@ bench(`bencode.encode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
2430
return result
2531
})
2632

27-
bench(`bencode.encode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
33+
bench.add(`bencode.encode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
2834
let result = null
2935

3036
run.start()
@@ -36,7 +42,7 @@ bench(`bencode.encode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
3642
return result
3743
})
3844

39-
bench(`bencode.encode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
45+
bench.add(`bencode.encode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
4046
let result = null
4147

4248
run.start()
@@ -48,7 +54,7 @@ bench(`bencode.encode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
4854
return result
4955
})
5056

51-
bench(`bencode.encode() [binary] ⨉ ${ITERATIONS}`, function (run) {
57+
bench.add(`bencode.encode() [binary] ⨉ ${ITERATIONS}`, function (run) {
5258
let result = null
5359

5460
run.start()
@@ -60,7 +66,7 @@ bench(`bencode.encode() [binary] ⨉ ${ITERATIONS}`, function (run) {
6066
return result
6167
})
6268

63-
bench(`bencode.decode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
69+
bench.add(`bencode.decode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
6470
let result = null
6571

6672
run.start()
@@ -72,7 +78,7 @@ bench(`bencode.decode() [buffer] ⨉ ${ITERATIONS}`, function (run) {
7278
return result
7379
})
7480

75-
bench(`bencode.decode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
81+
bench.add(`bencode.decode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
7682
let result = null
7783

7884
run.start()
@@ -84,7 +90,7 @@ bench(`bencode.decode() [utf8] ⨉ ${ITERATIONS}`, function (run) {
8490
return result
8591
})
8692

87-
bench(`bencode.decode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
93+
bench.add(`bencode.decode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
8894
let result = null
8995

9096
run.start()
@@ -96,7 +102,7 @@ bench(`bencode.decode() [ascii] ⨉ ${ITERATIONS}`, function (run) {
96102
return result
97103
})
98104

99-
bench(`bencode.decode() [binary] ⨉ ${ITERATIONS}`, function (run) {
105+
bench.add(`bencode.decode() [binary] ⨉ ${ITERATIONS}`, function (run) {
100106
let result = null
101107

102108
run.start()
@@ -107,3 +113,7 @@ bench(`bencode.decode() [binary] ⨉ ${ITERATIONS}`, function (run) {
107113

108114
return result
109115
})
116+
117+
await bench.run()
118+
119+
console.table(bench.table())

benchmark/buffer-vs-string.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import { fileURLToPath } from 'url'
34
import bencode from '../index.js'
4-
import bench from 'nanobench'
5+
6+
import { Bench } from 'tinybench'
7+
8+
const __filename = fileURLToPath(import.meta.url)
9+
const __dirname = path.dirname(__filename)
510

611
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
712
const str = buffer.toString('ascii')
813

914
const ITERATIONS = 10000
1015

11-
bench(`decode buffer ⨉ ${ITERATIONS}`, function (run) {
16+
const bench = new Bench({ time: 100 })
17+
18+
bench.add(`decode buffer ⨉ ${ITERATIONS}`, function (run) {
1219
let result = null
1320

1421
run.start()
@@ -20,7 +27,7 @@ bench(`decode buffer ⨉ ${ITERATIONS}`, function (run) {
2027
return result
2128
})
2229

23-
bench(`decode string ⨉ ${ITERATIONS}`, function (run) {
30+
bench.add(`decode string ⨉ ${ITERATIONS}`, function (run) {
2431
let result = null
2532

2633
run.start()
@@ -31,3 +38,7 @@ bench(`decode string ⨉ ${ITERATIONS}`, function (run) {
3138

3239
return result
3340
})
41+
42+
await bench.run()
43+
44+
console.table(bench.table())

benchmark/compare-decode.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import bench from 'nanobench'
3+
import { fileURLToPath } from 'url'
4+
import { Bench } from 'tinybench'
45

56
import bencode from '../index.js'
7+
68
import bencoding from 'bencoding'
79
import bncode from 'bncode'
810
import btparse from 'btparse'
9-
import dht from 'dht.js/lib/dht/bencode'
11+
import dht from 'dht.js/lib/dht/bencode.js'
1012
import dhtBencode from 'dht-bencode'
1113

14+
const __filename = fileURLToPath(import.meta.url)
15+
const __dirname = path.dirname(__filename)
16+
1217
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
1318

1419
const ITERATIONS = 10000
1520

16-
bench(`bencode.decode() ⨉ ${ITERATIONS}`, function (run) {
21+
const bench = new Bench({ time: 100 })
22+
23+
bench.add(`bencode.decode() ⨉ ${ITERATIONS}`, function (run) {
1724
let result = null
1825

1926
run.start()
@@ -25,7 +32,7 @@ bench(`bencode.decode() ⨉ ${ITERATIONS}`, function (run) {
2532
return result
2633
})
2734

28-
bench(`bencoding.decode() ⨉ ${ITERATIONS}`, function (run) {
35+
bench.add(`bencoding.decode() ⨉ ${ITERATIONS}`, function (run) {
2936
let result = null
3037

3138
run.start()
@@ -37,7 +44,7 @@ bench(`bencoding.decode() ⨉ ${ITERATIONS}`, function (run) {
3744
return result
3845
})
3946

40-
bench(`bncode.decode() ⨉ ${ITERATIONS}`, function (run) {
47+
bench.add(`bncode.decode() ⨉ ${ITERATIONS}`, function (run) {
4148
let result = null
4249

4350
run.start()
@@ -49,7 +56,7 @@ bench(`bncode.decode() ⨉ ${ITERATIONS}`, function (run) {
4956
return result
5057
})
5158

52-
bench(`btparse() ⨉ ${ITERATIONS}`, function (run) {
59+
bench.add(`btparse() ⨉ ${ITERATIONS}`, function (run) {
5360
let result = null
5461

5562
run.start()
@@ -61,7 +68,7 @@ bench(`btparse() ⨉ ${ITERATIONS}`, function (run) {
6168
return result
6269
})
6370

64-
bench(`dht.decode() ⨉ ${ITERATIONS}`, function (run) {
71+
bench.add(`dht.decode() ⨉ ${ITERATIONS}`, function (run) {
6572
let result = null
6673

6774
run.start()
@@ -73,7 +80,7 @@ bench(`dht.decode() ⨉ ${ITERATIONS}`, function (run) {
7380
return result
7481
})
7582

76-
bench(`dhtBencode.decode() ⨉ ${ITERATIONS}`, function (run) {
83+
bench.add(`dhtBencode.decode() ⨉ ${ITERATIONS}`, function (run) {
7784
let result = null
7885

7986
run.start()
@@ -84,3 +91,7 @@ bench(`dhtBencode.decode() ⨉ ${ITERATIONS}`, function (run) {
8491

8592
return result
8693
})
94+
95+
await bench.run()
96+
97+
console.table(bench.table())

benchmark/compare-encode.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import bench from 'nanobench'
3+
import { fileURLToPath } from 'url'
4+
import { Bench } from 'tinybench'
45

56
import bencode from '../index.js'
7+
68
import bencoding from 'bencoding'
79
import bncode from 'bncode'
8-
import dht from 'dht.js/lib/dht/bencode'
10+
import dht from 'dht.js/lib/dht/bencode.js'
911
import dhtBencode from 'dht-bencode'
1012

13+
const __filename = fileURLToPath(import.meta.url)
14+
const __dirname = path.dirname(__filename)
15+
1116
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
1217
const object = bencode.decode(buffer)
1318

1419
const ITERATIONS = 10000
1520

16-
bench(`bencode.encode() ⨉ ${ITERATIONS}`, function (run) {
21+
const bench = new Bench({ time: 100 })
22+
23+
bench.add(`bencode.encode() ⨉ ${ITERATIONS}`, function (run) {
1724
let result = null
1825

1926
run.start()
@@ -25,7 +32,7 @@ bench(`bencode.encode() ⨉ ${ITERATIONS}`, function (run) {
2532
return result
2633
})
2734

28-
bench(`bencoding.encode() ⨉ ${ITERATIONS}`, function (run) {
35+
bench.add(`bencoding.encode() ⨉ ${ITERATIONS}`, function (run) {
2936
let result = null
3037

3138
run.start()
@@ -37,7 +44,7 @@ bench(`bencoding.encode() ⨉ ${ITERATIONS}`, function (run) {
3744
return result
3845
})
3946

40-
bench(`bncode.encode() ⨉ ${ITERATIONS}`, function (run) {
47+
bench.add(`bncode.encode() ⨉ ${ITERATIONS}`, function (run) {
4148
let result = null
4249

4350
run.start()
@@ -49,7 +56,7 @@ bench(`bncode.encode() ⨉ ${ITERATIONS}`, function (run) {
4956
return result
5057
})
5158

52-
bench(`dht.encode() ⨉ ${ITERATIONS}`, function (run) {
59+
bench.add(`dht.encode() ⨉ ${ITERATIONS}`, function (run) {
5360
let result = null
5461

5562
run.start()
@@ -61,7 +68,7 @@ bench(`dht.encode() ⨉ ${ITERATIONS}`, function (run) {
6168
return result
6269
})
6370

64-
bench(`dhtBencode.encode() ⨉ ${ITERATIONS}`, function (run) {
71+
bench.add(`dhtBencode.encode() ⨉ ${ITERATIONS}`, function (run) {
6572
let result = null
6673

6774
run.start()
@@ -72,3 +79,7 @@ bench(`dhtBencode.encode() ⨉ ${ITERATIONS}`, function (run) {
7279

7380
return result
7481
})
82+
83+
await bench.run()
84+
85+
console.table(bench.table())

benchmark/encoding-length.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import { fileURLToPath } from 'url'
34
import bencode from '../index.js'
4-
import bench from 'nanobench'
5+
6+
import { Bench } from 'tinybench'
7+
8+
const __filename = fileURLToPath(import.meta.url)
9+
const __dirname = path.dirname(__filename)
510

611
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
712
const torrent = bencode.decode(buffer)
813

914
const ITERATIONS = 10000
1015

11-
bench('bencode.encodingLength(torrent)', function (run) {
16+
const bench = new Bench({ time: 100 })
17+
18+
bench.add('bencode.encodingLength(torrent)', function (run) {
1219
const result = null
1320

1421
run.start()
@@ -20,7 +27,7 @@ bench('bencode.encodingLength(torrent)', function (run) {
2027
return result
2128
})
2229

23-
bench('bencode.encodingLength(buffer)', function (run) {
30+
bench.add('bencode.encodingLength(buffer)', function (run) {
2431
const result = null
2532

2633
run.start()
@@ -32,7 +39,7 @@ bench('bencode.encodingLength(buffer)', function (run) {
3239
return result
3340
})
3441

35-
bench('bencode.encodingLength(string)', function (run) {
42+
bench.add('bencode.encodingLength(string)', function (run) {
3643
const result = null
3744

3845
run.start()
@@ -44,7 +51,7 @@ bench('bencode.encodingLength(string)', function (run) {
4451
return result
4552
})
4653

47-
bench('bencode.encodingLength(number)', function (run) {
54+
bench.add('bencode.encodingLength(number)', function (run) {
4855
const result = null
4956

5057
run.start()
@@ -56,7 +63,7 @@ bench('bencode.encodingLength(number)', function (run) {
5663
return result
5764
})
5865

59-
bench('bencode.encodingLength(array<number>)', function (run) {
66+
bench.add('bencode.encodingLength(array<number>)', function (run) {
6067
const result = null
6168

6269
run.start()
@@ -68,7 +75,7 @@ bench('bencode.encodingLength(array<number>)', function (run) {
6875
return result
6976
})
7077

71-
bench('bencode.encodingLength(small object)', function (run) {
78+
bench.add('bencode.encodingLength(small object)', function (run) {
7279
const result = null
7380

7481
run.start()
@@ -79,3 +86,7 @@ bench('bencode.encodingLength(small object)', function (run) {
7986

8087
return result
8188
})
89+
90+
await bench.run()
91+
92+
console.table(bench.table())

0 commit comments

Comments
 (0)