-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseBenchmark.ts
47 lines (46 loc) · 1.3 KB
/
parseBenchmark.ts
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
/* tslint:disable:no-console */
import * as Benchmark from "benchmark";
import { parse } from "../src/parser/parse";
import { formatBytes, generateBenchmarkInput } from "./utils";
const input = generateBenchmarkInput();
console.log("Size of test input: " + formatBytes(input.length));
const jsonStr = JSON.stringify(parse(input, true));
new Benchmark.Suite("compare")
.add("Parse Hashml", () => {
return parse(input);
})
.add("Parse JSON", () => {
return JSON.parse(jsonStr);
})
.add("String.matchAll", () => {
return [
...input.matchAll(
/(?:((?:\r\n|\n|\r|^)(\t*)[\t ]*(?:#([^ \[\r\n]+)(?: |$))?)|(#([^ \[]+)(\[)?)|(]\[)|(\[)|(\\(.)))/g
)
];
})
.add("Iterate through chars", () => {
const SHARP = "#";
const OPENING_BRACKET = "[";
let n = 0;
const end = input.length;
for (let i = 0; i < end; ++i) {
switch (input.charAt(i)) {
case SHARP:
case OPENING_BRACKET:
++n;
break;
}
}
return n;
})
// add listeners
.on("cycle", (event: Event) => {
console.log(String(event.target));
})
.on("complete", function (this: Benchmark[]) {
console.log("JSON/Hashml ratio: " + (this[1].stats.mean / this[0].stats.mean).toFixed(2));
console.log("Hashml throughput: " + formatBytes(input.length / this[0].stats.mean) + "/s");
})
// run async
.run({ async: true });