Skip to content
Eugene Lazutkin edited this page Mar 26, 2026 · 1 revision

The bench/ directory contains micro-benchmarks using nano-benchmark. They compare related components on generated datasets.

Running benchmarks

npm run bench -- bench/parser-jsonc.mjs
npm run bench -- bench/parser-jsonl.mjs
npm run bench -- bench/assembler-flex.mjs

Use nano-bench options after --: npm run bench -- bench/parser-jsonc.mjs --ms 200 --samples 50. Run npx nano-bench --help for details.

Benchmarks

Parser vs JSONC Parser

File: bench/parser-jsonc.mjs

Compares Parser and jsonc/Parser on the same ~100 KB JSON array. Both process identical input through chain() pipelines, measuring overhead of JSONC comment/trailing-comma support on plain JSON.

Typical result: no statistically significant difference. The JSONC parser's additional regex branches for //, /*, and trailing commas add negligible overhead when comments are absent.

Takeaway: use jsonc/Parser freely when input might contain comments — there is no measurable penalty on standard JSON.

Parser + StreamValues vs JSONL Parser

File: bench/parser-jsonl.mjs

Compares two ways to process JSONL (one JSON value per line):

  • parser({jsonStreaming: true}) + streamValues() — streaming tokenizer + assembler.
  • jsonl/Parser — line splitter + native JSON.parse().

Typical result: jsonl/Parser is ~10× faster.

Takeaway: for strict JSONL/NDJSON input where individual items fit in memory, always prefer jsonl/Parser. Use the streaming parser only when you need token-level access or when items are too large to parse in one shot. See also Performance § JSONL.

Assembler vs FlexAssembler

File: bench/assembler-flex.mjs

Feeds a pre-generated token array (~500 objects, ~10 000 tokens) to Assembler and FlexAssembler via consume(). Three variants:

Variant Description
assembler Standard Assembler — baseline.
flex-assembler FlexAssembler with no rules — measures structural overhead.
flex-assembler (Map rules) FlexAssembler with an always-matching object rule creating Maps.

Typical result: FlexAssembler (no rules) is ~8% slower than Assembler. With an always-matching Map rule, ~15% slower.

Takeaway: for plain object assembly, Assembler is slightly faster. FlexAssembler overhead is modest and scales with the number of rules that need to be checked. Use it when you need custom containers; otherwise prefer Assembler.

Dataset generation

All benchmarks generate data on the fly at import time — no large files are committed. Each file creates a ~50–100 KB payload of synthetic objects with mixed types (strings, numbers, booleans, arrays, nested objects).

Clone this wiki locally