-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbuffer-vs-string.js
More file actions
44 lines (31 loc) · 884 Bytes
/
buffer-vs-string.js
File metadata and controls
44 lines (31 loc) · 884 Bytes
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
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import bencode from '../index.js'
import { Bench } from 'tinybench'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
const str = buffer.toString('ascii')
const ITERATIONS = 10000
const bench = new Bench({ time: 100 })
bench.add(`decode buffer ⨉ ${ITERATIONS}`, function (run) {
let result = null
run.start()
for (let i = 0; i < ITERATIONS; i++) {
result = bencode.decode(buffer)
}
run.end()
return result
})
bench.add(`decode string ⨉ ${ITERATIONS}`, function (run) {
let result = null
run.start()
for (let i = 0; i < ITERATIONS; i++) {
result = bencode.decode(str)
}
run.end()
return result
})
await bench.run()
console.table(bench.table())