|
| 1 | +const { Suite } = require('benchmark'); |
| 2 | +const { table } = require('table'); |
| 3 | +const prettyMs = require('pretty-ms'); |
| 4 | +const { bold } = require('chalk'); |
| 5 | +const VerEx = require('../dist/verbalexpressions'); |
| 6 | + |
| 7 | +const benchmarks = {}; |
| 8 | + |
| 9 | +benchmarks.constructor = { |
| 10 | + VerEx: () => { VerEx(); }, |
| 11 | + RegExp: () => { RegExp(); }, |
| 12 | +}; |
| 13 | + |
| 14 | +benchmarks.startOfLine = { |
| 15 | + VerEx: () => { VerEx().startOfLine('a').test('alpha'); }, |
| 16 | + RegExp: () => { /^a/.test('alpha'); }, |
| 17 | +}; |
| 18 | + |
| 19 | +benchmarks.endOfLine = { |
| 20 | + VerEx: () => { VerEx().endOfLine('a').test('alpha'); }, |
| 21 | + RegExp: () => { /a$/.test('alpha'); }, |
| 22 | +}; |
| 23 | + |
| 24 | +benchmarks['then|find'] = { |
| 25 | + VerEx: () => { VerEx().find('alpha').test('alpha'); }, |
| 26 | + RegExp: () => { /alpha/.test('alpha'); }, |
| 27 | +}; |
| 28 | + |
| 29 | +benchmarks.maybe = { |
| 30 | + VerEx: () => { |
| 31 | + const expr = VerEx().maybe('a'); |
| 32 | + expr.test('a'); expr.test(''); |
| 33 | + }, |
| 34 | + RegExp: () => { |
| 35 | + const expr = /a?/; |
| 36 | + expr.test('a'); expr.test(''); |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +benchmarks.anything = { |
| 41 | + VerEx: () => { VerEx().anything().test('alpha'); }, |
| 42 | + RegExp: () => { /.*/.test('alpha'); }, |
| 43 | +}; |
| 44 | + |
| 45 | +benchmarks.anythingBut = { |
| 46 | + VerEx: () => { |
| 47 | + const expr = VerEx().anythingBut('a'); |
| 48 | + expr.test('alpha'); expr.test('epsilon'); |
| 49 | + }, |
| 50 | + RegExp: () => { |
| 51 | + const expr = /.*/; |
| 52 | + expr.test('alpha'); expr.test('epsilon'); |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +benchmarks.something = { |
| 57 | + VerEx: () => { |
| 58 | + const expr = VerEx().somethingBut('a'); |
| 59 | + expr.test('alpha'); expr.test(''); |
| 60 | + }, |
| 61 | + RegExp: () => { |
| 62 | + const expr = /.+/; |
| 63 | + expr.test('alpha'); expr.test(''); |
| 64 | + }, |
| 65 | +}; |
| 66 | + |
| 67 | +benchmarks.somethingBut = { |
| 68 | + VerEx: () => { |
| 69 | + const expr = VerEx().somethingBut('a'); |
| 70 | + expr.test('alpha'); expr.test('epsilon'); expr.test(''); |
| 71 | + }, |
| 72 | + RegExp: () => { |
| 73 | + const expr = /.+/; |
| 74 | + expr.test('alpha'); expr.test('epsilon'); expr.test(''); |
| 75 | + }, |
| 76 | +}; |
| 77 | + |
| 78 | +/* |
| 79 | + * TODO |
| 80 | + */ |
| 81 | + |
| 82 | +// benchmarks['anyOf|any'] = { VerEx: () => {}, RegExp: () => {} }; |
| 83 | +// benchmarks.not = { VerEx: () => {}, RegExp: () => {} }; |
| 84 | +// benchmarks.range = { VerEx: () => {}, RegExp: () => {} }; |
| 85 | +// benchmarks['lineBreak|br'] = { VerEx: () => {}, RegExp: () => {} }; |
| 86 | +// benchmarks.tab = { VerEx: () => {}, RegExp: () => {} }; |
| 87 | +// benchmarks.word = { VerEx: () => {}, RegExp: () => {} }; |
| 88 | +// benchmarks.digit = { VerEx: () => {}, RegExp: () => {} }; |
| 89 | +// benchmarks.whitespace = { VerEx: () => {}, RegExp: () => {} }; |
| 90 | +// benchmarks.addModifier = { VerEx: () => {}, RegExp: () => {} }; |
| 91 | +// benchmarks.removeModifier = { VerEx: () => {}, RegExp: () => {} }; |
| 92 | +// benchmarks.withAnyCase = { VerEx: () => {}, RegExp: () => {} }; |
| 93 | +// benchmarks.stopAtFirst = { VerEx: () => {}, RegExp: () => {} }; |
| 94 | +// benchmarks.searchOneLine = { VerEx: () => {}, RegExp: () => {} }; |
| 95 | +// benchmarks.repeatPrevious = { VerEx: () => {}, RegExp: () => {} }; |
| 96 | +// benchmarks.oneOrMore = { VerEx: () => {}, RegExp: () => {} }; |
| 97 | +// benchmarks.multiple = { VerEx: () => {}, RegExp: () => {} }; |
| 98 | +// benchmarks.beginCapture = { VerEx: () => {}, RegExp: () => {} }; |
| 99 | +// benchmarks.endCapture = { VerEx: () => {}, RegExp: () => {} }; |
| 100 | +// benchmarks.replace = { VerEx: () => {}, RegExp: () => {} }; |
| 101 | +// benchmarks.toRegExp = { VerEx: () => {}, RegExp: () => {} }; |
| 102 | + |
| 103 | +const suite = new Suite(); |
| 104 | + |
| 105 | +for (const [name, bench] of Object.entries(benchmarks)) { |
| 106 | + const VerExRun = bench.VerEx; |
| 107 | + const RegExpRun = bench.RegExp; |
| 108 | + |
| 109 | + suite |
| 110 | + .add(`VerEx ${name}`, VerExRun) |
| 111 | + .add(`RegExp ${name}`, RegExpRun); |
| 112 | +} |
| 113 | + |
| 114 | +const tableData = []; |
| 115 | + |
| 116 | +tableData.push([ |
| 117 | + bold('Description'), bold('Time'), bold('Uncertainty'), |
| 118 | +]); |
| 119 | + |
| 120 | +suite.on('cycle', (event) => { |
| 121 | + const { name, stats } = event.target; |
| 122 | + |
| 123 | + tableData.push([ |
| 124 | + name, |
| 125 | + prettyMs(stats.mean * 1000, { formatSubMs: true }), |
| 126 | + `±${stats.rme.toFixed(2)}%`, |
| 127 | + ]); |
| 128 | +}); |
| 129 | + |
| 130 | +suite.run(); |
| 131 | + |
| 132 | +const tableConfig = { |
| 133 | + drawHorizontalLine: index => index === 0 || index % 2 !== 0, |
| 134 | +}; |
| 135 | + |
| 136 | +console.log(table(tableData, tableConfig)); |
0 commit comments