|
| 1 | +import {readFileSync} from 'node:fs'; |
| 2 | +import {dirname, join} from 'node:path'; |
| 3 | +import {fileURLToPath} from 'node:url'; |
| 4 | +import {run, bench, group, summary} from 'mitata'; |
| 5 | +import * as ohm from 'ohm-js'; |
| 6 | + |
| 7 | +import es5js from '../../../examples/ecmascript/index.js'; |
| 8 | +import {makeEs5Matcher, wasmMatcherForGrammar} from '../test/_helpers.js'; |
| 9 | + |
| 10 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 11 | +const datadir = join(__dirname, '../test/data'); |
| 12 | + |
| 13 | +const inputs = { |
| 14 | + bookReview: readFileSync(join(datadir, '_book-review.liquid'), 'utf-8'), |
| 15 | + featuredProduct: readFileSync(join(datadir, '_featured-product.liquid'), 'utf-8'), |
| 16 | + footer: readFileSync(join(datadir, '_footer.liquid'), 'utf-8'), |
| 17 | + html5shiv: readFileSync(join(datadir, '_html5shiv-3.7.3.js'), 'utf-8'), |
| 18 | + underscore: readFileSync(join(datadir, '_underscore-1.8.3.js'), 'utf-8') |
| 19 | +}; |
| 20 | + |
| 21 | +const es5Matcher = await makeEs5Matcher(); |
| 22 | + |
| 23 | +function matchWithInput(m, input) { |
| 24 | + m.setInput(input); |
| 25 | + return m.match(); |
| 26 | +} |
| 27 | + |
| 28 | +const liquid = ohm.grammars(readFileSync(join(datadir, '_liquid-html.ohm'), 'utf8')); |
| 29 | +const liquidMatcher = await wasmMatcherForGrammar(liquid.LiquidHTML); |
| 30 | + |
| 31 | +group('ES5: html5shiv', () => { |
| 32 | + summary(() => { |
| 33 | + bench('Wasm', () => { |
| 34 | + matchWithInput(es5Matcher, inputs.html5shiv); |
| 35 | + }); |
| 36 | + bench('JS', () => { |
| 37 | + es5js.grammar.match(inputs.html5shiv); |
| 38 | + }); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +group('ES5: underscore', () => { |
| 43 | + summary(() => { |
| 44 | + bench('Wasm', () => { |
| 45 | + matchWithInput(es5Matcher, inputs.underscore); |
| 46 | + }); |
| 47 | + bench('JS', () => { |
| 48 | + es5js.grammar.match(inputs.underscore); |
| 49 | + }); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +group('LiquidHTML: book-review.liquid', () => { |
| 54 | + summary(() => { |
| 55 | + bench('Wasm', () => { |
| 56 | + matchWithInput(liquidMatcher, inputs.bookReview); |
| 57 | + }); |
| 58 | + bench('JS', () => { |
| 59 | + liquid.LiquidHTML.match(inputs.bookReview); |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +group('LiquidHTML: featured-product.liquid', () => { |
| 65 | + summary(() => { |
| 66 | + bench('Wasm', () => { |
| 67 | + matchWithInput(liquidMatcher, inputs.featuredProduct); |
| 68 | + }); |
| 69 | + bench('JS', () => { |
| 70 | + liquid.LiquidHTML.match(inputs.featuredProduct); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +group('LiquidHTML: footer.liquid', () => { |
| 76 | + summary(() => { |
| 77 | + bench('Wasm', () => { |
| 78 | + matchWithInput(liquidMatcher, inputs.footer); |
| 79 | + }); |
| 80 | + bench('JS', () => { |
| 81 | + liquid.LiquidHTML.match(inputs.footer); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +await run(); |
0 commit comments