-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
helper.js
83 lines (68 loc) · 1.88 KB
/
helper.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
'use strict';
var fs = require('fs');
var path = require('path');
var repeat = require('repeat-string');
function bench() {
var filepath = path.join(__dirname, 'last.md');
var str = fs.readFileSync(filepath, 'utf8');
var sections = str.split(/(?=\n?(?:# benchmark))/);
sections.shift();
var len = sections.length;
var idx = -1;
var res = [];
while (++idx < len) {
var section = sections[idx].trim();
var tok = parseSection(section);
if (tok) res.push(tok);
}
return res.join('\n');
}
function parseSection(str) {
var lines = str.split('\n').filter(Boolean);
if (!lines.length) return;
var title = lines.shift().trim();
var m = /^.*\/fixtures\/match\/([^(]+)/.exec(title);
var tok = {title: (m ? m[1] : title).trim()};
tok.title = tok.title.slice(0, tok.title.length - 3);
tok.micromatch = parseStats(lines);
tok['minimatch '] = parseStats(lines);
tok.multimatch = parseStats(lines);
var vals = values(tok);
var max = Math.max.apply(Math, vals);
var str = '';
str += ['#', tok.title].join(' ') + '\n';
str += format('micromatch', tok, max, 100);
str += format('minimatch ', tok, max, 100);
str += format('multimatch', tok, max, 100);
// console.log(str)
return str;
}
function parseStats(lines) {
var str = lines.shift().trim();
var m = /^([^\s]+)\s*x\s*([\d,.]+)/.exec(str);
if (!m) return 0;
var str = m[2];
var num = String(str).split(',').join('');
return {
num: num,
str: str
};
}
function values(obj) {
var vals = [];
for (var key in obj) {
if (key === 'title') continue;
vals.push(obj[key].num);
}
return vals;
}
function bar(tok, longest, diff) {
return repeat('█', (tok.num / longest) * 50);
}
function format(name, tok, max, diff) {
return [name, bar(tok[name], max, diff), '(' + tok[name].str + ' ops/sec)', '\n'].join(' ');
}
/**
* Expose `.bench` helper
*/
module.exports.bench = bench;