File: bench.js

package info (click to toggle)
node-marked 0.8.0%2Bds%2Brepack-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 1,948 kB
  • sloc: javascript: 3,640; sh: 16; makefile: 16
file content (298 lines) | stat: -rw-r--r-- 6,541 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const path = require('path');
const htmlDiffer = require('./helpers/html-differ.js');
const { loadFiles } = require('./helpers/load.js');

let marked = require('../lib/marked.js');
const es6marked = require('../src/marked.js');

/**
 * Load specs
 */
function load() {
  const dir = path.resolve(__dirname, './specs/commonmark');
  const sections = loadFiles(dir);
  let specs = [];

  for (const section in sections) {
    specs = specs.concat(sections[section].specs);
  }

  return specs;
}

/**
 * Run all benchmarks
 */
async function runBench(options) {
  options = options || {};
  const specs = load();

  // Non-GFM, Non-pedantic
  marked.setOptions({
    gfm: false,
    breaks: false,
    pedantic: false,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    marked.setOptions(options.marked);
  }
  await bench('es5 marked', specs, marked);

  es6marked.setOptions({
    gfm: false,
    breaks: false,
    pedantic: false,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    es6marked.setOptions(options.marked);
  }
  await bench('es6 marked', specs, es6marked);

  // GFM
  marked.setOptions({
    gfm: true,
    breaks: false,
    pedantic: false,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    marked.setOptions(options.marked);
  }
  await bench('es5 marked (gfm)', specs, marked);

  es6marked.setOptions({
    gfm: true,
    breaks: false,
    pedantic: false,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    es6marked.setOptions(options.marked);
  }
  await bench('es6 marked (gfm)', specs, es6marked);

  // Pedantic
  marked.setOptions({
    gfm: false,
    breaks: false,
    pedantic: true,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    marked.setOptions(options.marked);
  }
  await bench('es5 marked (pedantic)', specs, marked);

  es6marked.setOptions({
    gfm: false,
    breaks: false,
    pedantic: true,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    es6marked.setOptions(options.marked);
  }
  await bench('es6 marked (pedantic)', specs, es6marked);

  try {
    await bench('commonmark', specs, (() => {
      const commonmark = require('commonmark');
      const parser = new commonmark.Parser();
      const writer = new commonmark.HtmlRenderer();
      return function(text) {
        return writer.render(parser.parse(text));
      };
    })());
  } catch (e) {
    console.error('Could not bench commonmark. (Error: %s)', e.message);
  }

  try {
    await bench('markdown-it', specs, (() => {
      const MarkdownIt = require('markdown-it');
      const md = new MarkdownIt();
      return md.render.bind(md);
    })());
  } catch (e) {
    console.error('Could not bench markdown-it. (Error: %s)', e.message);
  }

  try {
    await bench('markdown.js', specs, (() => {
      const md = require('markdown').markdown;
      return md.toHTML.bind(md);
    })());
  } catch (e) {
    console.error('Could not bench markdown.js. (Error: %s)', e.message);
  }
}

async function bench(name, specs, engine) {
  const before = process.hrtime();
  for (let i = 0; i < 1e3; i++) {
    for (const spec of specs) {
      await engine(spec.markdown);
    }
  }
  const elapsed = process.hrtime(before);
  const ms = prettyElapsedTime(elapsed).toFixed();

  let correct = 0;
  for (const spec of specs) {
    if (await htmlDiffer.isEqual(spec.html, await engine(spec.markdown))) {
      correct++;
    }
  }
  const percent = (correct / specs.length * 100).toFixed(2);

  console.log('%s completed in %sms and passed %s%', name, ms, percent);
}

/**
 * A simple one-time benchmark
 */
async function time(options) {
  options = options || {};
  const specs = load();
  if (options.marked) {
    marked.setOptions(options.marked);
  }
  await bench('marked', specs, marked);
}

/**
 * Argument Parsing
 */
function parseArg(argv) {
  argv = argv.slice(2);

  const options = {};
  const orphans = [];

  function getarg() {
    let arg = argv.shift();

    if (arg.indexOf('--') === 0) {
      // e.g. --opt
      arg = arg.split('=');
      if (arg.length > 1) {
        // e.g. --opt=val
        argv.unshift(arg.slice(1).join('='));
      }
      arg = arg[0];
    } else if (arg[0] === '-') {
      if (arg.length > 2) {
        // e.g. -abc
        argv = arg.substring(1).split('').map(ch => {
          return `-${ch}`;
        }).concat(argv);
        arg = argv.shift();
      } else {
        // e.g. -a
      }
    } else {
      // e.g. foo
    }

    return arg;
  }

  const defaults = marked.getDefaults();

  while (argv.length) {
    const arg = getarg();
    switch (arg) {
      case '-t':
      case '--time':
        options.time = true;
        break;
      case '-m':
      case '--minified':
        options.minified = true;
        break;
      default:
        if (arg.indexOf('--') === 0) {
          const opt = camelize(arg.replace(/^--(no-)?/, ''));
          if (!defaults.hasOwnProperty(opt)) {
            continue;
          }
          options.marked = options.marked || {};
          if (arg.indexOf('--no-') === 0) {
            options.marked[opt] = typeof defaults[opt] !== 'boolean'
              ? null
              : false;
          } else {
            options.marked[opt] = typeof defaults[opt] !== 'boolean'
              ? argv.shift()
              : true;
          }
        } else {
          orphans.push(arg);
        }
        break;
    }
  }

  if (orphans.length > 0) {
    console.error();
    console.error('The following arguments are not used:');
    orphans.forEach(arg => console.error(`  ${arg}`));
    console.error();
  }

  return options;
}

/**
 * Helpers
 */
function camelize(text) {
  return text.replace(/(\w)-(\w)/g, (_, a, b) => a + b.toUpperCase());
}

/**
 * Main
 */
async function main(argv) {
  const opt = parseArg(argv);

  if (opt.minified) {
    marked = require('../marked.min.js');
  }

  if (opt.time) {
    await time(opt);
  } else {
    await runBench(opt);
  }
}

/**
 * returns time to millisecond granularity
 */
function prettyElapsedTime(hrtimeElapsed) {
  const seconds = hrtimeElapsed[0];
  const frac = Math.round(hrtimeElapsed[1] / 1e3) / 1e3;
  return seconds * 1e3 + frac;
}

if (!module.parent) {
  process.title = 'marked bench';
  main(process.argv.slice());
} else {
  exports = main;
  exports.main = main;
  exports.time = time;
  exports.runBench = runBench;
  exports.load = load;
  exports.bench = bench;
  module.exports = exports;
}