Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216/** Parse benchmark: jsonc-edit against microsoft `jsonc-parser` and `jsonc-eslint-parser`, on a clean document and on a commented document. jsonc-edit parses both structurally, so the two inputs differ only in the comment and trailing-comma work each parser must carry. Run with the `bench` task. @module */
import type { StringJsonc, } from '@monochromatic-dev/module-jsonc-edit/ts/brand.ts';import { parseJsonc, } from '@monochromatic-dev/module-jsonc-edit/ts';import { parseForESLint, } from 'jsonc-eslint-parser';import { parseTree, } from 'jsonc-parser';
//region Constants
/** Number of record entries in each benchmark input. */const ENTRY_COUNT = 300;
/** Timed iterations per parser. */const ITERATIONS = 3_000;
/** Untimed warm-up iterations to let the JIT settle. */const WARMUP = 200;
/** Milliseconds per second, for the ops/second figure. */const MS_PER_SECOND = 1_000;
//endregion Constants
//region Inputs
/** Builds a clean (comment-free, valid JSON) document of `count` entries. @param count - Number of record entries. @returns Pretty-printed JSON string. @example ```ts buildCleanInput({ count: 2 }); ``` */function buildCleanInput({ count,}: { readonly count: number;},): string { /** Accumulated entry lines. */ const lines: string[] = []; for (let index = 0; index < count; index += 1) lines.push(` "key${index}": { "id": ${index}, "name": "item-${index}", "tags": [1, 2, 3] }`,); return `{\n${lines.join(',\n',)}\n}`;}
/** Builds a commented document of `count` entries, forcing the structured parser. @param count - Number of record entries. @returns JSONC string with a trailing comment on each entry. @example ```ts buildCommentedInput({ count: 2 }); ``` */function buildCommentedInput({ count,}: { readonly count: number;},): string { /** Accumulated entry lines, each carrying a trailing comment. */ const lines: string[] = []; for (let index = 0; index < count; index += 1) lines.push(` "key${index}": { "id": ${index}, "name": "item-${index}" }, // entry ${index}`,); return `{\n${lines.join('\n',)}\n}`;}
//endregion Inputs
//region Parser adapters
/** Parses with jsonc-edit. @param source - Document to parse. @example ```ts runJsoncEdit('{}'); ``` */function runJsoncEdit(source: string,): void { parseJsonc({ source: source as StringJsonc, },);}
/** Parses with microsoft `jsonc-parser` into an offset tree. @param source - Document to parse. @example ```ts runMicrosoft('{}'); ``` */function runMicrosoft(source: string,): void { parseTree(source,);}
/** Parses with `jsonc-eslint-parser` into an ESTree-style AST. @param source - Document to parse. @example ```ts runEslint('{}'); ``` */function runEslint(source: string,): void { parseForESLint(source, { jsonSyntax: 'JSONC', },);}
//endregion Parser adapters
//region Harness
/** Times a parser over an input and prints its throughput. @param label - Parser label. @param parse - Parser adapter. @param input - Document to parse repeatedly. @example ```ts measure({ label: 'x', parse: runJsoncEdit, input: '{}' }); ``` */function measure({ label, parse, input,}: { readonly label: string; readonly parse: (source: string) => void; readonly input: string;},): void { for (let index = 0; index < WARMUP; index += 1) parse(input,); /** High-resolution start timestamp. */ const start = performance.now(); for (let index = 0; index < ITERATIONS; index += 1) parse(input,); /** Elapsed milliseconds across all timed iterations. */ const elapsed = performance.now() - start; /** Parses per second. */ const opsPerSecond = Math.round(ITERATIONS / (elapsed / MS_PER_SECOND),); console.log(` ${label.padEnd(36,)} ${`${elapsed.toFixed(1,)}ms`.padStart(9,)} ${String(opsPerSecond,).padStart(9,)} ops/s`,);}
//endregion Harness
//region Run
/** Clean benchmark document. */const clean = buildCleanInput({ count: ENTRY_COUNT, },);
/** Commented benchmark document. */const commented = buildCommentedInput({ count: ENTRY_COUNT, },);
console.log( `jsonc-edit parse benchmark: ${ENTRY_COUNT} entries, ${ITERATIONS} iterations\n` + `clean ${clean.length} B, commented ${commented.length} B\n`,);
console.log('CLEAN input (no comments or trailing commas):',);measure({ label: 'jsonc-edit parseJsonc', parse: runJsoncEdit, input: clean, },);measure({ label: 'microsoft jsonc-parser parseTree', parse: runMicrosoft, input: clean, },);measure({ label: 'jsonc-eslint-parser parseForESLint', parse: runEslint, input: clean, },);
console.log('\nCOMMENTED input (comments and trailing commas):',);measure({ label: 'jsonc-edit parseJsonc', parse: runJsoncEdit, input: commented, },);measure({ label: 'microsoft jsonc-parser parseTree', parse: runMicrosoft, input: commented, },);measure({ label: 'jsonc-eslint-parser parseForESLint', parse: runEslint, input: commented, },);
//endregion Run