Benchmarks
The same addon written three ways, each the idiomatic way of its framework:
| Implementation | Source | How it is written |
|---|---|---|
| napi-go | napigo/main.go |
//napi:export annotations, registrations generated by napi-go gen (Layer 2) |
| napi-rs | napi-rs/src/lib.rs |
#[napi], async fn on the tokio runtime of napi-rs |
| node-addon-api | node-addon-api/addon.cc |
C++ with the same argument checks, Napi::AsyncWorker (libuv thread pool) |
and plain JavaScript as a reference for the cost of the JS side of each case.
| Case | What it measures |
|---|---|
noop() |
the cost of a call |
add(a, b) |
two numbers in, one out |
echo(s) |
a string converted to the native language and back (16 B, 1 KiB, 64 KiB, ASCII) |
bufferLength(b) |
a 1 MiB Buffer read in place (no copy) |
makeBuffer(n) |
a new zero-filled 1 MiB Buffer returned to JS |
await resolveAsync(n), one at a time |
latency of a Promise resolved by a background thread |
resolveAsync(n) × 1000 with Promise.all |
throughput of Promises resolved by background threads |
Running
Requirements: Go with cgo, Node.js 22 or later; for napi-rs, Rust (cargo); for node-addon-api, a C++ compiler and the Node.js headers (on Linux and macOS: the official Node.js builds include them). An implementation that cannot be built is skipped.
cd bench && npm ci && cd ..
node bench/build.mjs # bench/build/{napigo,napi-rs,node-addon-api}.node
node bench/run.mjs # about two minutes
node bench/run.mjs --quick --filter echo # fewer samples, only the matching cases
node bench/run.mjs --json results.json
run.mjs first checks that every implementation returns the same results, and that the native
ones throw for an argument of the wrong type. It then calibrates each case so that a sample lasts
about 100 ms, interleaves the samples of the implementations (one round runs each once, so a change
of machine load affects all of them alike), discards three warm-up rounds and reports the median
of 15 samples. The CI workflow bench.yml runs it on Linux and
macOS and writes the table to the summary of the run.
Results
Linux x64 (2 vCPU, Intel Xeon @ 2.10 GHz), Node.js 22.23, Go 1.24.7, Rust 1.97, g++ 13.3, 2026-11-12. Times are medians per operation; lower is better.
| Case | napi-go | napi-rs | node-addon-api | plain JS |
|---|---|---|---|---|
| noop() | 136 ns | 29 ns | 24 ns | 4.0 ns |
| add(a, b) | 170 ns | 117 ns | 44 ns | 8.4 ns |
| echo(string of 16 B) | 237 ns | 213 ns | 121 ns | 7.4 ns |
| echo(string of 1 KiB) | 1.61 µs | 644 ns | 503 ns | 7.6 ns |
| echo(string of 64 KiB) | 44.1 µs | 22.7 µs | 22.6 µs | 7.2 ns |
| bufferLength(Buffer of 1 MiB) | 353 ns | 128 ns | 60 ns | 7.7 ns |
| makeBuffer(1 MiB) | 259 µs | 130 µs | 112 µs | 54.4 µs |
| await resolveAsync(n), one at a time | 46.6 µs | 30.2 µs | 31.1 µs | 52 ns |
| resolveAsync(n) × 1000 with Promise.all | 1.90 µs | 10.8 µs | 6.11 µs | 109 ns |
Between two runs the results differ by a few percent, except the Promise awaited one at a time, whose napi-go time ranged from 45 to 90 µs (it is the latency of waking a goroutine and then the JS thread, sensitive to scheduling on two CPUs).
Reading the results
- The floor of a call is the cgo callback, about 100 ns. Node.js calls the addon on its own
thread, which is not a Go thread: entering Go from C costs about 100 ns (
runtime.cgocallback), against a direct function call for Rust and C++. Every napi-go function pays it once; the conversions of numbers, booleans and short strings then run in C in the same crossing (the typed trampoline,sys/typed.c), which is whyaddcosts only 34 ns more thannoop. - Short strings are close to napi-rs; long ones are about twice as slow. Up to about 500 bytes
a string is read in C in the same crossing. A longer one falls back to Go
(
Value.GoString): a first read into a 256-byte buffer, a query of the length and a second read, each one a cgo crossing in which V8 scans the whole string. napi-rs and node-addon-api query the length and read once. []byteparameters are not handled by the typed trampoline: reading a Buffer takes two more crossings from Go (napi_is_typedarray,napi_get_typedarray_info), about 220 ns. The data is not copied: the cost does not depend on the size.- A returned
[]byteis copied.makeBufferallocates and clears 1 MiB on the Go heap, then Node-API copies it into a new Buffer (napi_create_buffer_copy); with allocations of this size the Go garbage collector runs about every third call (332 cycles in 1,000 calls withGODEBUG=gctrace=1). napi-rs hands itsVecto V8 without copying (external buffer). In napi-go,Env.NewBuffer(Layer 1) creates the Buffer in V8 memory and returns a[]byteview to fill, without the copy. - Promises in bulk are 3–6 times faster than napi-rs and node-addon-api. napi-go settles Promises through one threadsafe function per env with a Go queue, and wakes the JS thread only when the queue becomes non-empty: a thousand goroutines finishing together cost one wake-up. napi-rs and node-addon-api pay one per Promise. The latency of a single Promise is higher (a goroutine wakes up, then the JS thread).
Candidate optimizations, measured here: reading a long string with one length query and one read
(and without the 256-byte attempt when the typed trampoline has already found the string too long),
[]byte parameters in the typed trampoline, and returned []byte written into a Buffer allocated
by V8 instead of copied.