// linear.test.mjs — the parser's scans are linear (review M6). Each body // here is 80,000 characters of what made a scan quadratic: trailing // whitespace for cleanBody, and a line of unclosed links for the link // tests. The old code took from 2.7 to 10 seconds on each; linear code // takes milliseconds. The bound is generous, for CI machines. import { test } from 'node:test' import assert from 'node:assert/strict' import { cleanBody, spans, parse } from './markup.mjs' import { editor } from './testkit.mjs' import { serialise } from './fidelity.mjs' const size = 80000 const bound = 750 const bodies = { 'trailing spaces': 'x' + ' '.repeat(size) + 'y', 'trailing whitespace': '\t \n'.repeat(size / 3) + 'x', 'unclosed web links': '[a'.repeat(size / 2), 'web links with no address': '[a]('.repeat(size / 4), 'unclosed wiki links': '[[a'.repeat(size / 3), 'unclosed embeds': '![['.repeat(size / 3), 'closes only': ']]'.repeat(size / 2) } for (const [name, body] of Object.entries(bodies)) { test(`linear: ${name}`, () => { const start = performance.now() cleanBody(body) spans(body) parse(body) const { view, fidelity } = editor(body) assert.equal(serialise(view.state.doc, fidelity), cleanBody(body)) const took = performance.now() - start assert.ok(took < bound, `${name}: ${took.toFixed(0)}ms for ${size} characters`) }) }