# Performance benchmarks

Run the benchmark from a Git checkout with the MoonBit and Node.js versions
in `toolchain.json`. Put `moon` on PATH or set `MOON_BIN` and `MOON_HOME` to the
same installation.

```sh
node scripts/benchmark.mjs
node scripts/benchmark.mjs --debug
```

The default build mode is release. Each run builds the benchmark and the CLI
for the JS backend. It writes `_build/benchmark-release-summary.json` or
`_build/benchmark-debug-summary.json` with raw samples, min/median/max,
workload sizes, toolchain and host details, source SHA and working tree status.
The run removes each previous JS executable before building it. It requires
both the benchmark harness and CLI to be generated as nonempty files and
records their SHA-256 values before timing starts.
Save a copy of the report before running another version in the same mode.

| Workload | Fixed input | Timed operation |
| --- | --- | --- |
| Compile small and large | 26 and 15,401 ASCII characters | Compile an expression and read its source |
| Compiled filter | Three records | Search with a compiled expression, including JSON checks |
| Flat map | 60,000 numeric elements | Map the current value with a compiled expression |
| Nested map and projection | 128 arrays of 256 records, with every fourth field missing | Search with a compiled nested expression; map preserves missing values as null and projection omits them |
| Map budget and reference errors | 60,000 numeric elements | Check the first result under a zero-result budget or an invalid length reference |
| Structural equality | 100,000-element arrays, 20,000-member objects in reverse insertion order, or 128 arrays of 256 numbers | Compare with a compiled expression, including input validation |
| First/last array mismatch | The same 100,000-element arrays with one changed value | Compare with a compiled expression and verify false |
| Array membership | 100,000 scalars; 20,000 eight-number arrays or two-member objects; 1,024 arrays nested 64 levels; or an empty array | Search with compiled `contains`, including input validation; check early/late matches and missing values |
| Deep comparison controls | The 1,024-by-64 nested membership input, plus two 128-level singleton arrays | Compare missing/first-match membership with an input-validation control and direct equality |
| Single-member object comparisons | Two 128-level objects; 512 objects nested 64 levels with CJK/emoji keys; or 20,000 simple objects | Direct equality, deep missing/first membership, validation control and absent-key membership |
| Empty-object traversal | Two arrays of 20,000 empty objects, one empty object per operand, or two 20,000-member objects with the first left key absent | Compiled equality, including shape validation and expected-result checks |
| Large sort | 8,192 integers in a fixed permutation | Sort with a compiled expression |
| join / to_string | Short, Unicode, large and empty joins; container serialization and String identity | Compiled calls with preflight/result assertions; precise workload sizes in the comparison below |
| Numeric conversion | Short decimal; 65,534-unit overflow; 100,010-unit cancelling exponent; 65,554-unit significant-digit rounding; 65,544-unit split coefficient; 65,543-unit integer coefficient | Reuse compiled to_number, verify exact finite result or invalid_value before timing and during each operation |
| Long string | 524,288 UTF-16 units containing ASCII, CJK and emoji | Search with compiled `length(@)` |
| Wide object validation | 60,000 null members with CJK, emoji and escaped punctuation in keys | Validate input and search with compiled `length(@)` |
| Wide object serialization | The same 60,000-member object | Bounded compact serialization and output length |
| Wide array validation | 250,000 null elements | Validate input and search with compiled `length(@)` |
| Preserved-number serialization | 60,000 decimal and large-integer representations | Bounded serialization and output length |
| Compact serialization | 60,000 strings | Bounded serialization and output length |
| Batch compile | 512 copies of the same expression | Compile each expression |
| Batch repeated and distinct | 512 requests with equal-length expressions and identical results | Process the complete in-memory batch |
| Large batch | 8,192 repeated requests | Process the complete in-memory batch |
| Node startup | Empty program | Launch a fresh Node process |
| CLI filter and batch | Three records or 512 requests on stdin | Launch the CLI, decode input, query and write JSON |

Library inputs are prepared outside the timer. Each case has a dedicated
Node process and uses three warm-up samples and nine measured samples. A
sample averages the number of operations recorded in `iterationsPerSample`.
The harness checks expected output before timing and consumes a checksum
during the loop. The runner verifies the case name, iteration count and
checksum against the warm-up and measured work before recording samples.
Its `peakRssKiB` measures the full process, including setup
and warm-up. [Node.js resource usage reference](https://nodejs.org/docs/latest-v24.x/api/process.html#processresourceusage)

CLI samples time fresh child processes and check their JSON output after each
sample. Two warm-up launches precede nine measured launches. The Node startup
case gives a separate reference for interpreting this wall time. CLI and
startup peak RSS are omitted. Builds are timed separately.

For a change comparison, run both revisions with the same mode, toolchain and
machine under similar load. Compare the raw distributions and memory alongside
the median. Batch compilation in isolation provides a comparison point for
total batch cost. Different allocations and garbage collection can affect
each measurement. Use the regular verification command to check semantics and
resource boundaries. Performance measurements have no CI time threshold.

## String construction comparison (2026-12-06)

The 0.1.35 candidate removes join's temporary string array and per-part
StringView conversions. It first completes the existing element work,
type and UTF-16 size checks, then writes strings in a second pass. The
same candidate also charges non-string to_string serialization work
before allocation. Its preflight/output representation is unchanged.

The 0.1.34 runtime from `56c0f2c` and candidate used the same six new
API workloads on the local Windows x64 host, MoonBit 0.10.20+65efd5b93
and Node.js 24.14.0. Nine rounds alternated AB/BA order per case; each
fresh process used three warm-ups, nine measured samples and a 256 MiB
old-space limit. Values are medians of nine process medians; peak RSS
includes setup, input validation and JIT work.

| Case | Input | Baseline ms | Candidate ms | Baseline RSS KiB | Candidate RSS KiB |
| --- | --- | ---: | ---: | ---: | ---: |
| Many short joins | 60,000 one-character parts | 3.708125 | 3.044700 | 93,844 | 79,656 |
| Unicode joins | 20,000 CJK/emoji parts, slash/newline separator | 1.863175 | 1.511975 | 68,356 | 63,396 |
| Large parts | 64 parts of 8,192 units | 2.634050 | 2.621050 | 46,320 | 46,104 |
| Empty join | Empty input array | 0.000579 | 0.000586 | 51,188 | 51,140 |
| Container to_string | 2,048 records and metadata | 2.096050 | 2.050750 | 64,384 | 64,348 |
| String to_string | 65,536-unit existing string | 0.365069 | 0.363213 | 44,824 | 44,856 |

The two many-part joins improved by about 18–19% in local median time;
the controls were similar within overlapping process ranges. This is
workload-specific evidence, not a general speed or memory guarantee.
The standard release benchmark now includes all six cases, for 57 total
workloads. The shared benchmark source SHA-256 is
`745a5a6ea94c985936c7bcd3eaea99957eb7a97f921999d718df26b3bcab8e95`.
Raw paired processes and executable/source hashes are retained locally
under `_build/0135-string-pair/`.

## Long-number normalization comparison (2026-12-06)

The 0.1.34 candidate copies significant digits in at most two contiguous
suffix slices around the validated decimal point. It retains all digits and
the original syntax checks, exponent arithmetic and work fees. This removes
per-digit StringView creation and repeated JS string extension from that
part of normalization; the complete operation still scans O(n) input.

The 0.1.33 runtime from `d4efd2e` and the candidate used the same six-case
benchmark source, MoonBit 0.10.20+65efd5b93, Node.js 24.14.0 and local Windows
x64 host. Nine rounds alternated AB/BA order for each case. Every fresh
process used a 256 MiB old-space limit, three warm-ups and nine measured
samples with correctness/checksum assertions. Times below are medians of
the nine process medians; RSS is median full-process peak, including setup.

| Input / workload | Baseline ms | Candidate ms | Baseline RSS KiB | Candidate RSS KiB |
| --- | ---: | ---: | ---: | ---: |
| Short decimal | 0.000942 | 0.000881 | 52,680 | 52,276 |
| Long overflow | 1.419200 | 1.447300 | 58,024 | 57,844 |
| Cancelling exponent | 1.209950 | 1.222850 | 58,880 | 58,976 |
| Long fractional significand | 5.281950 | 3.625200 | 82,612 | 59,312 |
| Split integer/fraction significand | 5.583350 | 3.354650 | 80,552 | 59,448 |
| Integer significand | 5.546200 | 3.188400 | 82,500 | 59,304 |

The three measured long-significand workloads took about 31–43% less time,
with lower process RSS. The overflow/cancellation controls changed by about
1–2%, within their overlapping process ranges. These are local workload
measurements, not guarantees for other inputs or hosts. The standard release
benchmark now includes all six numeric cases, for 51 total workloads.

The shared benchmark source SHA-256 was
`239bbbc17f49c4c7553ec31da1055e8c3901c0c779e061a2dae0bcdfa47db776`.
Raw measurements and both executable/source hashes were retained locally
under `_build/0134-numeric-pair/`. They are not required to run the standard
benchmark or semantic verification.

## Empty-object traversal experiments (2026-12-06)

Three workloads now check equality of two arrays of 20,000 empty objects,
a single empty object per operand, and two 20,000-member objects whose first
left key is absent on the right. Each checks expected output before timing.

Two candidates were compared with the 0.1.28 runtime from `a7748fd`, using
the same new harness. One unified object traversal around its first member;
the other kept the existing paths and skipped iterator/frame creation only
for empty objects. Each initial comparison used nine alternating pairs of
fresh release processes on Windows x64, Node.js 24.14.0 and MoonBit
0.10.20+65efd5b93. Processes used a 256 MiB old-space limit, three warm-ups
and nine measured samples. Values below are medians of process medians.

| Candidate / set | Workload | Baseline (ms) | Candidate (ms) |
| --- | --- | ---: | ---: |
| Unified first-member entry | Empty-object arrays | 1.772700 | 1.548100 |
| Unified first-member entry | Deep-object first match | 3.276475 | 3.901550 |
| Unified first-member entry | Deep-object missing-member scan | 4.559775 | 6.416775 |
| Empty-object guard, initial | Empty-object arrays | 1.726625 | 1.315825 |
| Empty-object guard, initial | Wide-object member scan | 3.165350 | 3.289700 |
| Empty-object guard, initial | Deep-object missing-member scan | 3.835650 | 4.210350 |
| Empty-object guard, targeted repeat | Empty-object arrays | 1.797925 | 1.296675 |
| Empty-object guard, targeted repeat | Wide-object member scan | 3.381475 | 3.389875 |
| Empty-object guard, targeted repeat | Deep-object missing-member scan | 4.289025 | 4.363500 |

The guard's targeted repeat used another nine pairs over five workloads
with the same artifacts and settings. Its empty-object benefit was stable,
about 24–28% across sets. Initial control costs narrowed in the repeat:
the deep scan was about 10% higher initially and about 2% higher on repeat,
while the wide scan changed from about 4% higher to less than 1% higher.
Other controls also varied. These observations do not establish a stable
10% deep-scan regression or a general improvement.

Both candidates were withdrawn because an overall benefit was insufficiently
established. The published runtime remains 0.1.28. The final standard harness
is byte-identical to the experimental baseline artifact.
[Verification evidence](acceptance.md#empty-object-traversal-verification-2026-12-06)
records the retained tests and benchmark checks.

## Single-member object comparisons in 0.1.28

For a one-member object, equality takes the left member, charges its key's
UTF-16 length, looks it up in the right object, and continues with the
values. It keeps existing ancestors and avoids storing a continuation for
that member. Member retrieval still uses the core iterator.

The comparison uses runtime code from `49cce97` and `049ecc8` with the same
new harness. Nine pairs of fresh release processes alternated which build
ran first on Windows x64, Node.js 24.14.0 and MoonBit 0.10.20+65efd5b93.
Each process used a 256 MiB old-space limit, three warm-ups and nine measured
samples. Values are medians of process medians and peaks. The final standard
harness is byte-identical to the measured candidate.

| Initial workload | Before (ms) | Tail descent (ms) | Before peak RSS (KiB) | After peak RSS (KiB) |
| --- | ---: | ---: | ---: | ---: |
| Equality of two 128-level single-member objects | 0.0176908 | 0.0152526 | 60,720 | 60,596 |
| 512 objects nested 64 levels, missing member | 4.282500 | 3.860075 | 85,892 | 85,720 |
| Same shape, first member matches | 2.247775 | 2.390650 | 87,236 | 86,264 |
| Same missing-member input, `length(items)` control | 2.243675 | 2.169825 | 85,560 | 85,584 |
| 20,000 one-member objects, requested key absent | 1.832900 | 1.595925 | 68,984 | 68,924 |
| Existing two-member objects, last member matches | 3.213600 | 3.249050 | 71,880 | 71,904 |
| Existing 20,000-member object equality | 4.050850 | 4.005100 | 69,604 | 69,508 |
| Existing deep-array missing-member scan | 5.487700 | 5.473450 | 69,668 | 69,680 |
| Existing singleton-array equality | 0.0072509 | 0.0069535 | 52,652 | 51,856 |
| Existing three-record filter | 0.0018239 | 0.0019211 | 51,696 | 51,844 |

The first-match processes varied substantially, so five workloads were
repeated for a further nine alternating pairs with the same artifacts and
settings. Both measurement sets are retained.

| Targeted repeat | Before (ms) | Tail descent (ms) |
| --- | ---: | ---: |
| Single-member object equality | 0.0177941 | 0.0149212 |
| Deep-object missing-member scan | 4.155575 | 3.787000 |
| Deep-object first match | 2.315475 | 2.339600 |
| Input-validation/query control | 2.258375 | 2.107175 |
| Three-record filter | 0.0018109 | 0.0018512 |

Object equality took about 14–16% less time and deep missing-member scans
about 9–10% less across these sets. The absent-key scan improved about 13%
in the initial set. First-match medians were about 1–6% higher, and the tiny
filter about 2–5% higher. These tradeoffs are part of the retained result.
The validation control includes ordinary query/output checks and does not
isolate pure validation CPU time. Initial peak RSS differences ranged from
-972 to +148 KiB and include setup/warm-up. These host-specific results
establish no general memory or speed guarantee.
[Release verification](acceptance.md#single-member-object-comparisons-0128)
records the source, checks and registry installation.

## Singleton array comparisons in 0.1.27

When both arrays have one element, equality continues directly with their
children. Existing array/object ancestors remain on the traversal stack.
Every pair is still visited and charged; input/output validation and
short-circuit order are preserved.

The comparison uses runtime code from `45bc0d1` and `8193b67` with the same
new harness. Nine pairs of fresh release processes alternated which build
ran first on Windows x64, Node.js 24.14.0 and MoonBit 0.10.20+65efd5b93.
Each process used a 256 MiB old-space limit, three warm-ups and nine measured
samples. Values are medians of the nine process medians and peaks. The final
standard harness is byte-identical to the measured candidate artifact.

| Workload | Before (ms) | Tail descent (ms) | Before peak RSS (KiB) | After peak RSS (KiB) |
| --- | ---: | ---: | ---: | ---: |
| 1,024 arrays nested 64 levels, missing member | 5.392975 | 5.116975 | 70,588 | 69,612 |
| Same shape, first member matches | 3.135600 | 2.814900 | 71,528 | 71,404 |
| Same missing-member input, `length(items)` control | 2.637200 | 2.674325 | 71,484 | 71,368 |
| Equality of two 128-level singleton arrays | 0.0087003 | 0.0069223 | 52,780 | 51,964 |
| Existing eight-number arrays, last member matches | 4.220525 | 4.054800 | 87,976 | 88,296 |
| Existing two-member objects, last member matches | 3.138550 | 3.203150 | 71,720 | 71,812 |
| Existing 100,000-element equality | 3.139600 | 3.116100 | 66,012 | 66,036 |
| Existing three-record filter | 0.0019179 | 0.0017478 | 52,116 | 51,712 |
| Existing 100,000-scalar missing-member scan | 2.002800 | 2.034800 | 66,184 | 66,348 |

Direct singleton equality took about 20% less time, the deep missing-member
scan about 5%, and the deep first-match case about 10%. The object/scalar
control medians were about 2% higher. The `length(items)` control still
performs normal input/output validation and query evaluation; its timing
does not isolate pure validation CPU time. Process peak RSS includes setup
and warm-up; differences ranged from -976 to +320 KiB. Results describe
these workloads on this host and establish no general memory or speed
guarantee. See [release verification](acceptance.md#singleton-array-comparisons-0127).

## Array membership in 0.1.26

Array `contains` reuses a logical comparison stack within one function
invocation. Each candidate starts with an empty logical stack. The comparison
still creates container cursors/iterators as needed and preserves cumulative
work charges, Unicode-key/string charges and short-circuit order. Default
scratch for ordinary equality is fresh per call.

The comparison uses runtime code from `bd8d424` and `c6ecf17` with the same
new benchmark harness. Nine pairs of fresh release processes alternated
which build ran first on Windows x64, Node.js 24.14.0 and MoonBit
0.10.20+65efd5b93. Each process used a 256 MiB old-space limit, three warm-up
samples and nine measured samples. Values are medians of the nine process
medians and process peaks. Input validation remains inside the timed search.

| Workload | Before (ms) | Scratch reuse (ms) | Before peak RSS (KiB) | After peak RSS (KiB) |
| --- | ---: | ---: | ---: | ---: |
| 100,000 scalars, missing value | 2.05625 | 1.99618 | 66,240 | 66,264 |
| Empty candidate array | 0.0014323 | 0.0012454 | 50,248 | 50,356 |
| 20,000 eight-number arrays, first match | 2.47845 | 2.49203 | 88,112 | 88,548 |
| Same arrays, last match | 4.64210 | 4.25227 | 89,260 | 89,204 |
| Same arrays, missing value | 4.57832 | 4.29330 | 88,620 | 89,224 |
| 20,000 two-member objects, last match | 4.01605 | 3.35145 | 71,752 | 71,752 |
| 1,024 candidate arrays nested 64 levels, missing value | 6.53685 | 6.59163 | 70,228 | 70,456 |
| Existing three-record filter | 0.0019773 | 0.0018604 | 51,768 | 51,704 |
| Existing 100,000-element equality | 3.26783 | 3.15087 | 66,108 | 66,132 |

Long array scans took about 6–8% less time and the object scan about 17% less.
First-match and deep-array medians were about 1% higher. Tiny-query timings
varied across trials; the longer array/object scans support retaining this
change. Process peak RSS differences ranged from -64 to +604 KiB, including
setup and warm-up. This establishes neither a general memory reduction nor
a speed guarantee for other inputs or machines.
[Release verification](acceptance.md#array-membership-scratch-0126) records
the exact source, checks and registry installation.

## Structural equality in 0.1.25

The current source reuses one cursor per open array or object during equality
comparison. It preserves per-value work charges, UTF-16 string/key charges,
shared-reference occurrence counting and depth-first short-circuit order.

Measurements compare the runtime equality code from `26fb594` with
`25b6fdf`, using the same new benchmark harness for both builds. On Windows
x64, Node.js 24.14.0 and MoonBit 0.10.20+65efd5b93, each workload ran in nine
pairs of fresh release processes, alternating which build ran first. Each
process used a 256 MiB old-space limit and reported nine samples after three
warm-ups. Values below are medians of the nine process medians and peaks.

| Workload | Before (ms) | Cursor (ms) | Before peak RSS (KiB) | Cursor peak RSS (KiB) |
| --- | ---: | ---: | ---: | ---: |
| Equal 100,000-element arrays | 4.510 | 3.114 | 66,132 | 65,292 |
| Equal 20,000-member objects | 5.190 | 4.173 | 68,264 | 68,892 |
| Equal 128-by-256 nested arrays | 1.443 | 0.998 | 55,444 | 57,960 |
| Array mismatch at first element | 2.215 | 2.206 | 65,100 | 64,944 |
| Array mismatch at last element | 4.410 | 3.078 | 66,500 | 65,468 |
| Existing three-record filter | 0.001871 | 0.001914 | 51,216 | 51,232 |

The listed successful comparisons and late mismatch took about 20–31% less
time. First-element mismatch was similar. Input validation remains inside
each timed search, even when equality short-circuits. Peak RSS covers the
whole process: the nested case increased by 2,516 KiB and the object case
by 628 KiB. This change does not establish a general memory reduction or
a performance guarantee for other machines or workloads. The small filter
measurements provide context, with no demonstrated filter improvement.

These measurements describe the equality change published in Mooncakes
0.1.25 on 2026-12-06. See [verification evidence](acceptance.md#structural-equality-cursors-2026-12-05)
for the tested source, archive and fresh-consumer checks.

## Batch reuse in 0.1.19

The single-entry reuse slot improved repeated requests on a Windows x64 host
using Node.js 24.14.0 and MoonBit 0.10.20+65efd5b93. The comparison used the
release builds from `7039b33` and `9ee0f09`. Each workload ran in fresh child
processes for five pairs, alternating which revision ran first. Values are
the median of the five run medians and the median of the five process peaks.

| Batch workload | Before (ms) | After (ms) | Before peak RSS (KiB) | After peak RSS (KiB) |
| --- | ---: | ---: | ---: | ---: |
| 512 repeated expressions | 1.134 | 0.569 | 55,908 | 54,632 |
| 512 distinct expressions | 1.134 | 1.154 | 55,704 | 55,560 |
| 8,192 repeated expressions | 17.020 | 7.463 | 132,176 | 120,656 |

The distinct-expression paired timing ratios ranged from 0.984 to 1.053.
The chosen reuse slot holds one compiled expression of up to 1,024 UTF-16
units for the duration of the batch. It targets groups of identical requests
with little retained state. These measurements describe the listed workloads
on this host.
