-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
57 lines (51 loc) · 1.62 KB
/
index.ts
File metadata and controls
57 lines (51 loc) · 1.62 KB
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
import fs from 'fs'
import { join } from 'path'
import Benchmark, { Suite } from 'benchmark'
import chalk from 'chalk'
import { htmlEscape } from 'escape-goat'
import { escapeHTML, escapeHTMLBuf, asyncEscapeHTMLBuf } from '../index'
const fixture = fs.readFileSync(join(__dirname, 'fixture'), 'utf8')
const miniFixture = '<div>{props.getNumber()}</div>'
const fixtureBuffer = Buffer.from(fixture)
const miniFixtureBuffer = Buffer.from(miniFixture)
const largeSuite = new Suite('Large input')
const miniSuite = new Suite('Small input')
function run(suite: Suite, fx: string, fxBuffer: Buffer) {
return new Promise((resolve) => {
suite
.add('napi', () => {
escapeHTML(fx)
})
.add('napi#buff', () => {
escapeHTMLBuf(fxBuffer)
})
.add(
'napi#asyncBuff',
async (defer: any) => {
await asyncEscapeHTMLBuf(fxBuffer)
defer.resolve()
},
{
defer: true,
},
)
.add('javascript', () => {
htmlEscape(fx)
})
.on('cycle', function (event: Benchmark.Event) {
// @ts-expect-error
event.target.name = `${event.target.name} @ ${suite.name}`
console.info(String(event.target))
})
.on('complete', function (this: Benchmark.Target & Benchmark.Suite) {
console.info(
`${this.name} bench suite: Fastest is ${chalk.green(
this.filter('fastest').map((t: Benchmark.Target) => t.name),
)}`,
)
resolve()
})
.run()
})
}
run(largeSuite, fixture, fixtureBuffer).then(() => run(miniSuite, miniFixture, miniFixtureBuffer))