-
Notifications
You must be signed in to change notification settings - Fork 5
/
benchmark.js
61 lines (49 loc) · 1.15 KB
/
benchmark.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { precise } from 'precise'
import { Lru } from './dist/toad-cache.mjs'
const nth = 2e3
const cache = new Lru(nth)
const data = new Array(nth)
function seed() {
let i = -1
while (++i < nth) {
data[i] = Math.floor(Math.random() * nth)
}
}
function populate(arg, start = 0) {
const pnth = arg.max
let i = -1
while (++i < pnth) {
arg.set(i + start, data[i])
}
}
function get(arg, start = 0) {
const gnth = arg.max
let i = -1
while (++i < gnth) {
arg.get(i + start, data[i])
}
}
function bench(n = 0, x = 1, type = 'set') {
if (type === 'set') {
seed()
const timer = precise().start()
populate(cache, n)
timer.stop()
console.log(
`Run ${x} ${x === 1 ? 'Set' : 'Evict'} (${n === 0 ? 'Low Keys' : 'High Keys'}): ${
timer.diff() / 1e6
} ms`,
)
} else if (type === 'get') {
const timer = precise().start()
get(cache, n)
timer.stop()
console.log(`Run ${x} Get (${n === 0 ? 'Low Keys' : 'High Keys'}): ${timer.diff() / 1e6} ms`)
}
}
console.log(`Benchmarking ${nth} items (random value per run)`)
bench()
bench(nth, 2)
bench(void 0, 3)
bench(nth, 4)
bench(nth, 5, 'get')