11import fs from 'fs'
22import path from 'path'
3- import bench from 'nanobench'
3+ import { fileURLToPath } from 'url'
4+ import { Bench } from 'tinybench'
45
56import bencode from '../index.js'
67
8+ const __filename = fileURLToPath ( import . meta. url )
9+ const __dirname = path . dirname ( __filename )
10+
711const buffer = fs . readFileSync ( path . join ( __dirname , 'test.torrent' ) )
812const object = bencode . decode ( buffer )
913const objectUtf8 = bencode . decode ( buffer , 'utf8' )
@@ -12,7 +16,9 @@ const objectBinary = bencode.decode(buffer, 'binary')
1216
1317const 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 ( ) )
0 commit comments