-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.js
executable file
·48 lines (37 loc) · 1.45 KB
/
bench.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
#!/usr/bin/env node
const {BufferedRegExpScanner, Dialect, TokenDefinition} = require('./dist')
// import {BufferedRegExpScanner, Dialect, TokenDefinition} from './src'
const length = 100000
const subject = Array(length).fill(`if (this) then {that}`, 0, length).join('\n')
const myLang = new Dialect([
new TokenDefinition('IF', String.raw`if`),
new TokenDefinition('THIS', String.raw`this`),
new TokenDefinition('THEN', String.raw`then`),
new TokenDefinition('SPACE', String.raw`\s+`),
new TokenDefinition('LPAREN', String.raw`\(`),
new TokenDefinition('RPAREN', String.raw`\)`),
new TokenDefinition('LBRACE', String.raw`\{`),
new TokenDefinition('RBRACE', String.raw`\}`),
new TokenDefinition('VAR', String.raw`\w+`),
])
let scanner = new BufferedRegExpScanner(subject)
console.log(`scanning ${Math.floor(subject.length / 1024)}KB equivalent file`)
const t0 = Date.now()
for (const _tok of scanner.generateTokensUsingDialect(myLang)) {
/* nada */
}
console.log(`raw scan: ${Date.now() - t0}ms`)
const tokens = []
scanner = new BufferedRegExpScanner(subject)
const t1 = Date.now()
for (const tok of scanner.generateTokensUsingDialect(myLang)) {
tokens.push(tok)
}
console.log(`scan: ${Date.now() - t1}ms`)
const t2 = Date.now()
tokens[Math.floor(length / 2)].line
console.log(`first lineno: ${Date.now() - t2}ms`)
const t3 = Date.now()
tokens[Math.floor(length / 2)].line
console.log(`second lineno: ${Date.now() - t3}ms`)
/* eslint no-console: off */