-
Notifications
You must be signed in to change notification settings - Fork 14
/
benchmark.js
56 lines (47 loc) · 1.58 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
const fs = require('fs')
const path = require('path')
const {
loadWASM,
OnigScanner,
OnigString
} = require('.');
const wasmBin = fs.readFileSync(path.join(__dirname, './lib/onigasm.wasm')).buffer
const StopWatch = typeof performance === 'undefined' ? Date : performance
const log = console.log
loadWASM(wasmBin).then(() => {
const str = fs.readFileSync('node_modules/typescript/lib/typescriptServices.js', 'utf8').repeat(2)
const scanner = new OnigScanner(['searchTillTheEndButYouWontFindMe'])
var T0 = StopWatch.now()
log()
const test = (charCount) => {
let t0 = StopWatch.now()
let stepCount = 50
let stepFactor = charCount / 5
for (let i = 0; i < 100; i++) {
for (let j = 0; j < stepCount; j++) {
scanner.findNextMatchSync(str.slice(0, j * stepFactor))
}
}
log(`Uncached strings < ${charCount} characters\n:`, StopWatch.now() - t0, 'ms')
t0 = StopWatch.now()
var onig_strs = Array(stepCount).fill(undefined).map((_, i) => new OnigString(str.slice(0, stepFactor * i)))
for (let i = 0; i < 100; i++) {
for(let j = 0; j < stepCount; j++) {
scanner.findNextMatchSync(onig_strs[j])
}
}
log(`Cached strings < ${charCount} characters\n:`, StopWatch.now() - t0, 'ms')
log()
}
[
100,
1000,
10000,
50000,
100000,
1000000,
2000000,
].forEach(test)
console.log('All tests took ', StopWatch.now() - T0, 'ms')
})
.catch(console.log)