|
| 1 | +import BenchTable from 'benchtable' |
| 2 | +import cliProgress from 'cli-progress' |
| 3 | +import * as fs from 'fs' |
| 4 | +import MarkdownIt from 'markdown-it' |
| 5 | +import { compiler as latestCompiler } from 'markdown-to-jsx-latest' |
| 6 | +import path from 'path' |
| 7 | +import ReactMarkdown from 'react-markdown' |
| 8 | +import rehypeRaw from 'rehype-raw' |
| 9 | +import rehypeReact from 'rehype-react' |
| 10 | +import { remark } from 'remark' |
| 11 | +import remarkDirective from 'remark-directive' |
| 12 | +import remarkGfm from 'remark-gfm' |
| 13 | +import remarkRehype from 'remark-rehype' |
| 14 | +import SimpleMarkdown from 'simple-markdown' |
| 15 | +import { compiler } from './dist/index.module.js' |
| 16 | +// @ts-ignore - react/jsx-runtime types may be incomplete |
| 17 | +import * as prod from 'react/jsx-runtime' |
| 18 | + |
| 19 | +const { version: latestVersion } = JSON.parse( |
| 20 | + fs.readFileSync( |
| 21 | + path.join( |
| 22 | + import.meta.dirname, |
| 23 | + 'node_modules/markdown-to-jsx-latest/package.json' |
| 24 | + ), |
| 25 | + 'utf8' |
| 26 | + ) |
| 27 | +) |
| 28 | + |
| 29 | +const mdIt = new MarkdownIt() |
| 30 | +const suite = new BenchTable() |
| 31 | + |
| 32 | +const fixture = fs.readFileSync('./src/fixture.md', 'utf8') |
| 33 | + |
| 34 | +// Set up rehype processor with all plugins to match markdown-to-jsx features |
| 35 | +// @ts-ignore - rehype-react types may be incomplete |
| 36 | +const production = { Fragment: prod.Fragment, jsx: prod.jsx, jsxs: prod.jsxs } |
| 37 | + |
| 38 | +const rehypeProcessor = remark() |
| 39 | + .use(remarkGfm) // GFM tables, task lists, autolinks, strikethrough, footnotes |
| 40 | + .use(remarkDirective) // Directives like [!NOTE] (though may not match exactly) |
| 41 | + .use(remarkRehype, { allowDangerousHtml: true }) // Convert mdast to hast |
| 42 | + .use(rehypeRaw) // Process HTML in markdown |
| 43 | + .use(rehypeReact, production) // Convert hast to React |
| 44 | + |
| 45 | +// Create a parse-only processor for AST benchmarks |
| 46 | +const remarkParseProcessor = remark() |
| 47 | + .use(remarkGfm) // GFM tables, task lists, autolinks, strikethrough, footnotes |
| 48 | + .use(remarkDirective) |
| 49 | + |
| 50 | +const bar = new cliProgress.SingleBar( |
| 51 | + { |
| 52 | + clearOnComplete: true, |
| 53 | + }, |
| 54 | + cliProgress.Presets.shades_classic |
| 55 | +) |
| 56 | +let totalCycles |
| 57 | + |
| 58 | +const isAll = process.argv.includes('--all') |
| 59 | +const isJsx = process.argv.includes('--jsx') |
| 60 | +// const isHtml = process.argv.includes('--html') |
| 61 | + |
| 62 | +type Test = { |
| 63 | + name: string |
| 64 | + fn: (input: any) => any |
| 65 | +} |
| 66 | + |
| 67 | +const parseTests = [ |
| 68 | + { |
| 69 | + name: 'markdown-to-jsx (next) [parse]', |
| 70 | + fn: input => compiler(input, { ast: true }), |
| 71 | + }, |
| 72 | + { |
| 73 | + name: `markdown-to-jsx (${latestVersion}) [parse]`, |
| 74 | + fn: input => latestCompiler(input, { ast: true }), |
| 75 | + }, |
| 76 | + isAll && { |
| 77 | + name: 'rehype [parse]', |
| 78 | + fn: input => remarkParseProcessor.processSync(input), |
| 79 | + }, |
| 80 | + isAll && { |
| 81 | + name: 'simple-markdown [parse]', |
| 82 | + fn: input => SimpleMarkdown.defaultBlockParse(input), |
| 83 | + }, |
| 84 | + isAll && { |
| 85 | + name: 'markdown-it [parse]', |
| 86 | + fn: input => mdIt.parse(input), |
| 87 | + }, |
| 88 | +].filter(Boolean) as Test[] |
| 89 | + |
| 90 | +const jsxTests = [ |
| 91 | + { |
| 92 | + name: 'markdown-to-jsx (next) [jsx]', |
| 93 | + fn: input => compiler(input), |
| 94 | + }, |
| 95 | + { |
| 96 | + name: `markdown-to-jsx (${latestVersion}) [jsx]`, |
| 97 | + fn: input => latestCompiler(input), |
| 98 | + }, |
| 99 | + isAll && { |
| 100 | + name: 'rehype [jsx]', |
| 101 | + fn: input => rehypeProcessor.processSync(input).result, |
| 102 | + }, |
| 103 | + isAll && { |
| 104 | + name: 'simple-markdown [jsx]', |
| 105 | + fn: input => |
| 106 | + SimpleMarkdown.defaultReactOutput( |
| 107 | + SimpleMarkdown.defaultBlockParse(input) |
| 108 | + ), |
| 109 | + }, |
| 110 | + isAll && { |
| 111 | + name: 'react-markdown [jsx]', |
| 112 | + fn: input => ReactMarkdown({ children: input }), |
| 113 | + }, |
| 114 | +].filter(Boolean) as Test[] |
| 115 | + |
| 116 | +const htmlTests = [ |
| 117 | + isAll && { |
| 118 | + name: 'markdown-it [html]', |
| 119 | + fn: input => mdIt.render(input), |
| 120 | + }, |
| 121 | +].filter(Boolean) as Test[] |
| 122 | + |
| 123 | +async function setupBenchmark() { |
| 124 | + let evals = suite |
| 125 | + |
| 126 | + for (const test of parseTests) { |
| 127 | + evals.addFunction(test.name, test.fn) |
| 128 | + } |
| 129 | + |
| 130 | + if (isAll || isJsx) { |
| 131 | + for (const test of jsxTests) { |
| 132 | + evals.addFunction(test.name, test.fn) |
| 133 | + } |
| 134 | + |
| 135 | + // if (isAll || isHtml) { |
| 136 | + // for (const test of htmlTests) { |
| 137 | + // evals.addFunction(test.name, test.fn) |
| 138 | + // } |
| 139 | + // } |
| 140 | + } |
| 141 | + |
| 142 | + evals |
| 143 | + .addInput('simple markdown string', ['_Hello_ **world**!']) |
| 144 | + .addInput('large markdown string', [fixture]) |
| 145 | + .on('start', () => { |
| 146 | + totalCycles = suite._counter |
| 147 | + bar.start(totalCycles, 0) |
| 148 | + }) |
| 149 | + .on('cycle', () => bar.update(totalCycles - suite._counter)) |
| 150 | + .on('abort error complete', () => bar.stop()) |
| 151 | + .on('complete', function () { |
| 152 | + console.log('Fastest is ' + suite.filter('fastest').map('name')) |
| 153 | + console.log(suite.table.toString()) |
| 154 | + }) |
| 155 | + // run async |
| 156 | + .run({ async: true }) |
| 157 | +} |
| 158 | + |
| 159 | +setupBenchmark() |
0 commit comments