-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathbench.js
executable file
·92 lines (79 loc) · 2.5 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env node
const Benchmark = require('benchmark').Benchmark
const fs = require('fs')
const Path = require('path')
const commonmark = require('commonmark')
const Showdown = require('showdown')
const marked = require('marked')
const markdownit = require('markdown-it')('commonmark')
const markdown_wasm = require('../../dist/markdown.node.js')
// setup markdownit
// disable expensive IDNa links encoding:
const markdownit_encode = markdownit.utils.lib.mdurl.encode;
markdownit.normalizeLink = function(url) { return markdownit_encode(url); };
markdownit.normalizeLinkText = function(str) { return str; };
// setup showdown
var showdown = new Showdown.Converter()
// setup commonmark
var parser = new commonmark.Parser()
var renderer = new commonmark.HtmlRenderer()
// parse CLI input
let filename = process.argv[2]
if (!filename) {
console.error(`usage: bench.js <markdown-file>`)
console.error(`usage: bench.js <dir-of-markdown-files>`)
process.exit(1)
}
// print CSV header
console.log(csv(["library","file","ops/sec","filesize"]))
// run tests on all files in a directory or a single file
let st = fs.statSync(filename)
if (st.isDirectory()) {
process.chdir(filename)
for (let fn of fs.readdirSync(".")) {
benchmarkFile(fn)
}
} else {
benchmarkFile(filename)
}
// Benchmark.options.maxTime = 10
function csv(values) {
return values.map(s => String(s).replace(/,/g,"\\,")).join(",")
}
function benchmarkFile(benchfile) {
var contents = fs.readFileSync(benchfile, 'utf8');
var contentsBuffer = fs.readFileSync(benchfile);
let csvLinePrefix = `${benchfile.replace(/,/g,"\\,")},${contentsBuffer.length},`
new Benchmark.Suite({
onCycle(ev) {
let b = ev.target
// console.log("cycle", b)
console.log(csv([b.name, benchfile, b.hz, contentsBuffer.length]))
},
// onComplete(ev) {
// let b = ev.target
// console.log("onComplete", {ev}, b.stats, b.times)
// }
}).add('commonmark', function() {
renderer.render(parser.parse(contents));
})
.add('showdown', function() {
showdown.makeHtml(contents);
})
.add('marked', function() {
marked(contents);
})
.add('markdown-it', function() {
markdownit.render(contents);
})
.add('markdown-wasm', function() {
markdown_wasm.parse(contentsBuffer, {asMemoryView:true});
})
// .add('markdown-wasm/string', function() {
// markdown_wasm.parse(contents);
// })
// .add('markdown-wasm/bytes', function() {
// markdown_wasm.parse(contentsBuffer, {asMemoryView:true});
// })
.run();
}