# Benchmarks

Side-by-side comparisons of node-ctypes with other FFI libraries, calling the
same C functions of the shared test fixture, and microbenchmarks of the
node-ctypes JavaScript layer. They measure performance only and are not run by
the test suite. The method and the published results are in
[Performance](../../guides/performance.md).

| File                                             | What it does                                                                      |
| ------------------------------------------------ | --------------------------------------------------------------------------------- |
| [benchmark_koffi.js](./benchmark_koffi.js)       | node-ctypes vs Koffi (`npm run bench:koffi`, Node 24+)                            |
| [benchmark_node_ffi.js](./benchmark_node_ffi.js) | node-ctypes vs `node:ffi` (`npm run bench:ffi`, Node 26+)                         |
| [benchmark_js_paths.js](./benchmark_js_paths.js) | node-ctypes JavaScript-layer hot paths (`npm run bench:js-paths`)                 |
| [ab_js_paths.js](./ab_js_paths.js)               | A/B of two node-ctypes builds on those paths (`npm run bench:js-paths:ab`)        |
| [runner.js](./runner.js)                         | sampling, console table, JSON (`--output`) and HTML report of the FFI comparisons |
| [charts.js](./charts.js)                         | SVG charts for the guides from a JSON report (`npm run bench:charts`)             |
| [benchmark_python.py](./benchmark_python.py)     | standalone Python ctypes timings (`npm --prefix tests run bench:python`)          |
| `results/`                                       | default HTML reports, ignored by Git                                              |

## Running a comparison

From the repository root, after building node-ctypes:

```bash
npm --prefix tests install          # once: installs Koffi
npm run build:test-fixture          # the C functions both libraries call
npm run bench:koffi                 # table on the console, results/koffi.html
```

Run one comparison at a time, on an otherwise idle machine. `--filter`,
`--samples`, `--duration` and `--output` are described in
[Running the comparisons](../../guides/performance.md#running-the-comparisons);
the workloads of each comparison in the [test suite guide](../README.md).

## Updating the published results

The charts and tables of [Performance](../../guides/performance.md) come from
one run of `bench:koffi`. To replace them with a new run:

1. Run the comparison saving the JSON report (this also writes `koffi.html`
   next to it):

   ```bash
   npm run bench:koffi -- --output tests/benchmarks/results/koffi.json
   ```

2. Regenerate the charts. This overwrites `guides/images/koffi-*.svg`: the
   ratio summary and one chart per workload group, with light and dark colors.
   File names depend only on the competitor and the group, so the links in the
   page stay valid.

   ```bash
   npm run bench:charts -- tests/benchmarks/results/koffi.json
   ```

   With `--out <directory>` the charts are written elsewhere, for example to
   look at them first, or for a run on another machine that must not replace
   the published one.

3. Update `guides/performance.md` by hand; `charts.js` does not edit Markdown:
   - the description of the run under **Results**: node-ctypes, Node.js and
     Koffi versions, the machine (also whether it is a virtual machine) and the
     date;
   - the tables: the node-ctypes and Koffi times and the ratio, from the
     console table or from `median_ns` of `ctypes` and `other` in the JSON;
   - for a new workload group, a `###` heading with its image
     (`images/koffi-<group>.svg`) and its table, like the others.

4. Check the site with `npm run docs` (no warnings; the SVG files are copied to
   `docs/media`), then commit `guides/images/` and `guides/performance.md`.
   The JSON and HTML reports stay in `results/`, ignored by Git.

A `bench:ffi` report (`--output tests/benchmarks/results/node-ffi.json`) gives
`node-ffi-*.svg` charts in the same way; the guides do not show them yet.

## JavaScript hot paths and A/B comparisons

`benchmark_js_paths.js` times the JavaScript paths that the FFI comparisons do
not reach: pointer creation and access (`pointer()`, `fromAddress`, `.address`,
`.contents`, `p[0]`, pointer fields), pointer instances and `byref()` passed to
`POINTER(T)` parameters, `errcheck`, `HRESULT`, `use_errno`/`use_last_error`,
`SimpleCData` arguments, ctypes arrays (construction, `from_buffer`, arrays of
arrays), `struct()`/`union()` creation and array-field reads, `Structure`
array fields, callbacks receiving `POINTER(T)` arguments and prototype
instantiation. The `call_aggregate` cases cover plain-object point and HFA
arguments, plus synchronous and sequential asynchronous point results.
Each case checks its result before it is timed.

```bash
npm run build:test-fixture
npm run bench:js-paths                          # console table
npm run bench:js-paths -- --filter pointer,call_ # only matching cases
npm run bench:js-paths -- --json                 # one JSON object
```

One run in one process is a smoke test, not a measurement for a decision:
noise between processes (about ±3 ns for a synchronous call, up to ±15% for an
asynchronous one) is large compared with the differences that matter. To decide
whether a change of the addon or of `lib/` is acceptable, compare the two
builds with `ab_js_paths.js`:

```bash
# A = baseline checkout (for example a git worktree), B = the working tree;
# each lib/index.js loads the addon from its own build/ folder.
npm run bench:js-paths:ab -- --a ../baseline/lib/index.js --b lib/index.js --pairs 5
```

The script follows this method:

- each measurement runs in a fresh process, so the V8 heap, GC and libuv pool
  of one variant do not affect the other;
- pairs alternate the order, A then B and B then A, with at least 5 pairs per
  order;
- for each case it reports the median B/A ratio and its interquartile range
  separately for each order;
- a case more than 5% slower in **both** orders rejects variant B: a gain in
  other cases does not compensate it;
- a case whose interquartile range exceeds 5% is reported as noisy: add pairs
  instead of deciding.

Exit codes: `0` all cases passed, `1` at least one confirmed regression,
`2` invalid input or samples, `3` inconclusive (no confirmed regression, but
at least one noisy case). Empty filters/results and differing case sets are
not successful comparisons. Leave `NODE_CTYPES_NATIVE_PATH` unset: the two
processes must resolve their own addons.

Otherwise apply B only if its behavior is unchanged, and report the median and
spread of each case. Measure in Release, without probes, with a runtime and
addon native to the measured architecture: numbers taken under emulation (x64
Node on Windows ARM64) are indicative only. On Windows ARM64 build the native
addon with `cmake-js compile --CDFFI_ARCHITECTURE=aarch64`.
