| 1 | // linear.test.mjs — the parser's scans are linear (review M6). Each body |
| 2 | // here is 80,000 characters of what made a scan quadratic: trailing |
| 3 | // whitespace for cleanBody, and a line of unclosed links for the link |
| 4 | // tests. The old code took from 2.7 to 10 seconds on each; linear code |
| 5 | // takes milliseconds. The bound is generous, for CI machines. |
| 6 | |
| 7 | import { test } from 'node:test' |
| 8 | import assert from 'node:assert/strict' |
| 9 | import { cleanBody, spans, parse } from './markup.mjs' |
| 10 | import { editor } from './testkit.mjs' |
| 11 | import { serialise } from './fidelity.mjs' |
| 12 | |
| 13 | const size = 80000 |
| 14 | const bound = 750 |
| 15 | |
| 16 | const bodies = { |
| 17 | 'trailing spaces': 'x' + ' '.repeat(size) + 'y', |
| 18 | 'trailing whitespace': '\t \n'.repeat(size / 3) + 'x', |
| 19 | 'unclosed web links': '[a'.repeat(size / 2), |
| 20 | 'web links with no address': '[a]('.repeat(size / 4), |
| 21 | 'unclosed wiki links': '[[a'.repeat(size / 3), |
| 22 | 'unclosed embeds': '![['.repeat(size / 3), |
| 23 | 'closes only': ']]'.repeat(size / 2) |
| 24 | } |
| 25 | |
| 26 | for (const [name, body] of Object.entries(bodies)) { |
| 27 | test(`linear: ${name}`, () => { |
| 28 | const start = performance.now() |
| 29 | cleanBody(body) |
| 30 | spans(body) |
| 31 | parse(body) |
| 32 | const { view, fidelity } = editor(body) |
| 33 | assert.equal(serialise(view.state.doc, fidelity), cleanBody(body)) |
| 34 | const took = performance.now() - start |
| 35 | assert.ok(took < bound, `${name}: ${took.toFixed(0)}ms for ${size} characters`) |
| 36 | }) |
| 37 | } |
| 38 | |