# Benchmarks

These numbers are a reference for the order of magnitude and for catching regressions, not a promise. Run them on your hardware before you size anything.

## Machine and versions

| | |
| --- | --- |
| CPU | AMD Ryzen 7 7435HS (8 cores, 16 threads), laptop, frequency scaling on |
| Memory | 15 GiB |
| OS | Linux 7.3.3 (CachyOS) |
| Rust | 1.99.0, `--release` bench profile |
| criterion | 0.5.1 |
| Node | 22.23.4 |
| Bun | 1.4.4 |
| Strig | 0.6.1 plus the unreleased changes of the day, `tz` feature on as by default |

A laptop under normal desktop load: expect 10% to 20% of noise between runs on the slower cases. Two runs of the Rust suite gave 12.8 and 14.7 µs for the same case. Treat smaller differences as noise.

## What is measured

All cases share one catalog (`document.view` and `document.edit` on `document` and `project`, broad target `org`).

| Case | What it does |
| --- | --- |
| `decide/simple` | One `decide` against one role grant on the org. |
| `decide/conditions_and_relations` | One `decide` of `document.edit` against about 1,000 grants: one guarded by an `eq` and a `time_between` condition, one through the `editor` relation (50 tuples), 1,000 unrelated grants the engine has to skip and, in Rust only, 10 deny locks. |
| `list/plan` | `list` of what the subject can edit among `document`s: the plan of the filter, with the same grants and 50 tuples. |
| `list/resolve_10k_rows` | `list` plus resolving the filter over 10,000 resources in memory (in JS, one call with `resources`). |
| `apply/governance` | One `Add` of a grant through `Governance` with `CatalogConformance` and `NoSelfManagement`, no audit. |
| `apply/governance_with_audit` | The same, audited at the `standard` level. In Rust to a `JsonLinesAudit` over `io::sink()`. In JS the record is built and returned, with no sink. |
| `apply/governance_with_audit_sink` | JS only: the same, with an `auditSink` that does nothing, to price the call into JavaScript. |

The JS numbers include what crosses the boundary: the request and the result are serialized as JSON, and a `decide` takes the `Context` that already holds the grants. That cost, not the decision, is most of the difference between a Rust call and a JS call on the small cases.

## Rust

Median of the criterion estimate.

| Case | Time |
| --- | ---: |
| `decide/simple` | 479 ns |
| `decide/conditions_and_relations` | 14.7 µs |
| `list/plan` | 41.1 µs |
| `list/resolve_10k_rows` | 4.02 ms (about 400 ns per row) |
| `apply/governance` | 410 ns |
| `apply/governance_with_audit` | 1.01 µs |

The cost of a decision grows with the grants that name the same permission and kind, not with the size of the catalog: the 1,000 unrelated grants are most of what separates the two decide cases. The audit adds about 0.6 µs per change at `standard`. The `forensic` level also hashes with SHA-256, so it costs more. Listing filters 10,000 rows in memory in the time of about 8,000 single `decide` calls of the simple case. `strig-postgres` turns the same plan into SQL instead of resolving rows in memory.

## JavaScript

Median of seven samples of 500 ms each, after 300 ms of warm-up. This table was taken before the boundary change described [below](#javascript-the-boundary-before-and-after); `decide` and `apply` are faster now, and the second table has the new numbers for the small cases.

| Case | Node, native | Node, wasm | Bun, native | Bun, wasm |
| --- | ---: | ---: | ---: | ---: |
| `decide/simple` | 16.1 µs | 19.6 µs | 12.6 µs | 22.0 µs |
| `decide/conditions_and_relations` | 30.9 µs | 42.4 µs | 31.6 µs | 41.0 µs |
| `list/plan` | 248 µs | 300 µs | 232 µs | 303 µs |
| `list/resolve_10k_rows` | 87.1 ms | 92.7 ms | 77.2 ms | 103 ms |
| `apply/governance` | 28.9 µs | 34.1 µs | 24.0 µs | 39.1 µs |
| `apply/governance_with_audit` | 40.3 µs | 48.4 µs | 36.4 µs | 53.0 µs |
| `apply/governance_with_audit_sink` | 45.3 µs | 57.3 µs | 38.4 µs | 82.0 µs |

Reading them:

- Before the change, the native module cost 16 µs for a simple decision, most of it the boundary, against 0.5 µs in Rust.
- The wasm backend is about 1.2 to 1.4 times slower than native on Node. Bun's wasm shows the widest spread, so treat its figures as rough. This is also what a Linux musl or Alpine install runs on ([JavaScript](https://strig.vercel.app/docs/javascript.md#install-by-platform)).
- `list/resolve_10k_rows` includes sending 10,000 resources into the engine. It is the cost of an exact in-memory filter from JS.
- An `auditSink` adds one JavaScript call per record, about 5 µs on Node native.

## JavaScript: the boundary, before and after

A `check` on Node native spent its time like this, when `decide/simple` took 16.1 µs against 0.48 µs in Rust:

| Step | Cost |
| --- | ---: |
| An empty N-API call | 0.25 µs |
| Reading the request object, property by property, about 0.7 µs per leaf | about 12 µs |
| The engine | 0.5 µs |
| `JSON.stringify` of the same request | 0.77 µs |
| Parsing that string in Rust | 0.73 µs |

The `Context` already held the grants on the Rust side, so the grants were never the cost; the request and the response were. The wasm glue showed the same shape (`check` 15.6 µs).

The binding now crosses with one string (`JSON.stringify` in, `JSON.parse` out for `decide` and `explain`), and `checkMany` spreads the call over a batch. With 100 requests per batch (median):

| Case | Node, native | Node, wasm | Bun, native | Bun, wasm |
| --- | ---: | ---: | ---: | ---: |
| `decide/simple` | 6.5 µs | 9.3 µs | 5.1 µs | 8.6 µs |
| `check/simple` | 3.5 µs | 4.5 µs | 2.6 µs | 3.3 µs |
| `checkMany/simple`, per decision | 2.5 µs | 3.7 µs | 2.5 µs | 3.2 µs |

A single `decide` stays above 2 µs on Node native. A `check` costs at least 0.77 µs to stringify, 0.73 µs to parse, 0.49 µs in the engine and 0.25 µs for the call, about 2.3 µs, and a `decide` adds the building, serialising and parsing of the `Decision`, about 1.6 µs. Going lower needs a request that is not text, such as handles to a subject and a resource prepared once, which is a different API. For one request, `check` is the cheapest answer. For many, `checkMany`.

## Reproduce

Rust, from the repository root:

```text title="Terminal"
export CARGO_TARGET_DIR=/path/with/space   # optional: the release build and criterion need a few GB
cargo bench -p strig --bench engine
```

criterion prints the estimate per case and keeps its reports under `target/criterion`. `cargo bench -p strig --bench listing` prints listing against a per-row `check` for growing tables, and the cost of each kind of condition.

JavaScript, from `crates/strig-node/js`:

```text title="Terminal"
sh ../scripts/build.sh all        # the native module and the wasm
npm ci
npm run build:ts
node bench/bench.mjs              # both backends; --backend native|wasm picks one
bun bench/bench.mjs
```

The `Benchmarks` workflow in GitHub Actions, run by hand, does the same on a GitHub runner. It is not part of CI, and runner numbers are noisier than a dedicated machine, so compare only runs from the same kind of machine.
