typenative 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +2 -0
- package/CHANGELOG.md +258 -0
- package/CONTRIBUTION.md +1 -1
- package/README.md +17 -1
- package/bin/cli.js +26 -11
- package/bin/go/bridge.js +74 -14
- package/bin/go/convert.js +59 -1
- package/bin/go/metadata.js +13 -4
- package/bin/go/overrides.js +4 -6
- package/bin/go/stdlib.generated.js +2 -0
- package/bin/index.js +338 -47
- package/bin/main.js +36 -14
- package/bin/node/assert.js +2 -2
- package/bin/node/buffer.js +9 -0
- package/bin/node/child_process.js +51 -3
- package/bin/node/cluster.js +41 -3
- package/bin/node/core.js +1 -1
- package/bin/node/crypto.js +205 -4
- package/bin/node/dgram.js +230 -6
- package/bin/node/dns.js +471 -8
- package/bin/node/errno_tables.js +445 -0
- package/bin/node/events.js +1 -1
- package/bin/node/fs.js +22 -15
- package/bin/node/fs_watch.js +407 -0
- package/bin/node/http.js +128 -30
- package/bin/node/http2.js +37 -7
- package/bin/node/module_v8.js +142 -0
- package/bin/node/net.js +9 -6
- package/bin/node/process.js +242 -0
- package/bin/node/readline.js +272 -1
- package/bin/node/registry.js +3 -1
- package/bin/node/sqlite.js +60 -12
- package/bin/node/stream.js +145 -10
- package/bin/node/test.js +311 -19
- package/bin/node/timers.js +1 -1
- package/bin/node/typed_arrays.js +608 -0
- package/bin/node/url.js +23 -5
- package/bin/node/util.js +21 -1
- package/bin/node/util_extra.js +6 -30
- package/bin/node/worker_threads.js +79 -19
- package/bin/node/zlib.js +131 -16
- package/bin/transpiler.js +4729 -616
- package/bin/typecheck-node.js +1 -0
- package/bin/version.js +1 -1
- package/conformance.md +1030 -877
- package/examples/README.md +54 -0
- package/examples/fibonacci/README.md +89 -0
- package/examples/fibonacci/bench.mjs +87 -0
- package/examples/fibonacci/main.ts +21 -0
- package/examples/hello-world/README.md +62 -0
- package/examples/hello-world/bench.mjs +66 -0
- package/examples/hello-world/main.ts +7 -0
- package/examples/json-server/README.md +127 -0
- package/examples/json-server/bench.mjs +263 -0
- package/examples/json-server/main.ts +67 -0
- package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
- package/govendor/github.com/andybalholm/brotli/README.md +19 -0
- package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
- package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
- package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
- package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
- package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
- package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
- package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
- package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
- package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
- package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
- package/govendor/github.com/andybalholm/brotli/command.go +254 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
- package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
- package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
- package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
- package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
- package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
- package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
- package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
- package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
- package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
- package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
- package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
- package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
- package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
- package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
- package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
- package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
- package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
- package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
- package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
- package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
- package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
- package/govendor/github.com/andybalholm/brotli/http.go +201 -0
- package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
- package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
- package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
- package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
- package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
- package/govendor/github.com/andybalholm/brotli/params.go +37 -0
- package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
- package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
- package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
- package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
- package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
- package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
- package/govendor/github.com/andybalholm/brotli/state.go +313 -0
- package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
- package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
- package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
- package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
- package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
- package/govendor/github.com/andybalholm/brotli/util.go +7 -0
- package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
- package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
- package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
- package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
- package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
- package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
- package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
- package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
- package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
- package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
- package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
- package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +1 -1
- package/types/typenative-go.d.ts +23 -0
- package/types/typenative-npm.d.ts +241 -5
- package/types/typenative.d.ts +302 -12
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
Small, complete TypeNative programs, each runnable as-is under **both**
|
|
4
|
+
Node.js and a compiled TypeNative binary, with a `bench.mjs` that measures
|
|
5
|
+
Node vs TypeNative on the same workload and a README documenting the results.
|
|
6
|
+
|
|
7
|
+
| Example | Demonstrates | Headline result (this machine*) |
|
|
8
|
+
|----------------------------------------------|--------------------------------------------------------------------------|--------------------------------------------------|
|
|
9
|
+
| [hello-world](hello-world/) | `console.log`, `process.argv`, template literals | **7.0x faster startup** (14.7 ms vs 103.5 ms) |
|
|
10
|
+
| [fibonacci](fibonacci/) | Recursion, `perf_hooks` timing, CPU-bound number crunching | **1.9x faster compute** (50.2 ms vs 94.8 ms) |
|
|
11
|
+
| [json-server](json-server/) | `node:http` web server with GET + POST JSON routes, zero dependencies | **1.3x higher HTTP throughput** (17.8k vs 13.4k GET req/s) |
|
|
12
|
+
|
|
13
|
+
\* Windows 11 Pro, AMD Ryzen 5 5500U, Node v24.19.0, Go 1.26.0, TypeNative
|
|
14
|
+
0.4.0. See each example's README for full methodology and numbers.
|
|
15
|
+
|
|
16
|
+
## Running an example
|
|
17
|
+
|
|
18
|
+
From a checkout of this repository (requires Node.js ≥ 24 and Go ≥ 1.26; run
|
|
19
|
+
`npm ci && npm run build` once if `bin/cli.js` does not exist yet):
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
# compile + run, one step
|
|
23
|
+
node bin/cli.js --source examples/hello-world/main.ts --script
|
|
24
|
+
|
|
25
|
+
# or compile a distributable binary
|
|
26
|
+
node bin/cli.js --source examples/hello-world/main.ts --output dist/hello.exe
|
|
27
|
+
./dist/hello.exe
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
With the published npm package, run the same commands from inside an example
|
|
31
|
+
folder using `npx typenative` instead of `node bin/cli.js`.
|
|
32
|
+
|
|
33
|
+
Under plain Node.js, the examples run directly too:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
cd examples/fibonacci
|
|
37
|
+
node main.ts 35
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Reproducing the benchmarks
|
|
41
|
+
|
|
42
|
+
Each example folder contains a self-contained harness that compiles the
|
|
43
|
+
example with the repo's own CLI and compares both runtimes:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
cd examples/hello-world && node bench.mjs
|
|
47
|
+
cd examples/fibonacci && node bench.mjs 35 15
|
|
48
|
+
cd examples/json-server && node bench.mjs
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Across the three, TypeNative wins process startup (~7x), CPU-bound compute
|
|
52
|
+
(~2x) and HTTP throughput (~1.3x, with lower median latency). The READMEs
|
|
53
|
+
report what was measured, including run-to-run variation and how each load
|
|
54
|
+
was generated.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Fibonacci
|
|
2
|
+
|
|
3
|
+
The classic CPU-bound benchmark: naive recursive Fibonacci. The same source
|
|
4
|
+
runs unchanged under Node.js and as a compiled TypeNative binary, and times
|
|
5
|
+
itself with `performance.now()` so both runtimes are measured the same way —
|
|
6
|
+
compute time only, startup excluded.
|
|
7
|
+
|
|
8
|
+
## The code
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
// main.ts
|
|
12
|
+
import { performance } from 'node:perf_hooks';
|
|
13
|
+
|
|
14
|
+
function fib(n: number): number {
|
|
15
|
+
return n < 2 ? n : fib(n - 1) + fib(n - 2);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const n = Number(process.argv[2] ?? '35');
|
|
19
|
+
|
|
20
|
+
const start = performance.now();
|
|
21
|
+
const result = fib(n);
|
|
22
|
+
const elapsedMs = performance.now() - start;
|
|
23
|
+
|
|
24
|
+
console.log(`fib(${n}) = ${result}`);
|
|
25
|
+
console.log(`computed in ${elapsedMs.toFixed(2)} ms`);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Run it
|
|
29
|
+
|
|
30
|
+
With Node.js:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
node main.ts 35
|
|
34
|
+
# fib(35) = 9227465
|
|
35
|
+
# computed in 94.81 ms
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
With TypeNative (compile to a native binary and run it):
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
npx typenative --source main.ts --output dist/fib.exe
|
|
42
|
+
./dist/fib.exe 35
|
|
43
|
+
# fib(35) = 9227465
|
|
44
|
+
# computed in 50.16 ms
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
From a checkout of this repository, use the local CLI instead of `npx`:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
node bin/cli.js --source examples/fibonacci/main.ts --script # compile + run
|
|
51
|
+
node bin/cli.js --source examples/fibonacci/main.ts --output dist/fib.exe
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
(`--script` compiles and runs in one step; passing a custom `n` requires a
|
|
55
|
+
built binary, as shown above.)
|
|
56
|
+
|
|
57
|
+
## Benchmark: Node.js vs TypeNative
|
|
58
|
+
|
|
59
|
+
Reproduce with `node bench.mjs [n=35] [runs=15]`. The script compiles
|
|
60
|
+
`main.ts` with the TypeNative CLI, runs `fib(35)` under both runtimes, and
|
|
61
|
+
reports two numbers per run: the program's own in-process compute time
|
|
62
|
+
(parsed from its output) and the total wall time including process startup.
|
|
63
|
+
|
|
64
|
+
**Environment:** Windows 11 Pro, AMD Ryzen 5 5500U, Node v24.19.0, Go 1.26.0,
|
|
65
|
+
TypeNative 0.4.0, default (non-`--release`) build.
|
|
66
|
+
|
|
67
|
+
Compute time, `fib(35)` over 15 runs (measured in-program):
|
|
68
|
+
|
|
69
|
+
| Runtime | min | median | mean | max |
|
|
70
|
+
|------------|-----------|-----------|-----------|-----------|
|
|
71
|
+
| Node.js | 93.53 ms | 94.81 ms | 96.43 ms | 101.84 ms |
|
|
72
|
+
| TypeNative | 42.61 ms | 50.16 ms | 49.57 ms | 54.02 ms |
|
|
73
|
+
|
|
74
|
+
Total wall time per run, including startup (spawn to exit):
|
|
75
|
+
|
|
76
|
+
| Runtime | min | median | mean | max |
|
|
77
|
+
|------------|-----------|-----------|-----------|-----------|
|
|
78
|
+
| Node.js | 197.57 ms | 203.96 ms | 209.96 ms | 227.60 ms |
|
|
79
|
+
| TypeNative | 63.76 ms | 65.49 ms | 66.83 ms | 78.75 ms |
|
|
80
|
+
|
|
81
|
+
**TypeNative computes 1.9x faster** and finishes the whole run **3.1x
|
|
82
|
+
faster**. The recursive calls, arithmetic, and number boxing all compile
|
|
83
|
+
ahead of time to native machine code via Go, while V8 must tier up through
|
|
84
|
+
its JIT before it reaches comparable speed — and never reaches it here, since
|
|
85
|
+
Go's float64 arithmetic and non-virtual calls map directly onto the hardware.
|
|
86
|
+
|
|
87
|
+
Takeaway: for CPU-bound TypeScript (number crunching, parsing, simulations),
|
|
88
|
+
compiling with TypeNative roughly doubles throughput without changing a line
|
|
89
|
+
of code.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// CPU benchmark: Node.js vs TypeNative for the fibonacci program.
|
|
2
|
+
//
|
|
3
|
+
// Compiles main.ts to a native binary with the TypeNative CLI from this repo
|
|
4
|
+
// checkout (../../bin/cli.js), then runs the same program under both runtimes.
|
|
5
|
+
// Each run reports two numbers:
|
|
6
|
+
// - compute time: parsed from the program's own `computed in X ms` output
|
|
7
|
+
// (measured inside the program with process.hrtime.bigint)
|
|
8
|
+
// - total wall time: process spawn to exit, measured here
|
|
9
|
+
//
|
|
10
|
+
// Usage: node bench.mjs [n=35] [runs=15]
|
|
11
|
+
|
|
12
|
+
import { spawnSync } from 'node:child_process';
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
|
|
17
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
const repoRoot = path.resolve(here, '..', '..');
|
|
19
|
+
const cli = path.join(repoRoot, 'bin', 'cli.js');
|
|
20
|
+
const n = Number(process.argv[2] ?? '35') || 35;
|
|
21
|
+
const runs = Number(process.argv[3] ?? '15') || 15;
|
|
22
|
+
const exeName = process.platform === 'win32' ? 'fib.exe' : 'fib';
|
|
23
|
+
const exePath = path.join(here, 'dist', exeName);
|
|
24
|
+
|
|
25
|
+
function stats(samples) {
|
|
26
|
+
const sorted = [...samples].sort((a, b) => a - b);
|
|
27
|
+
const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
|
|
28
|
+
return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function fmt(ms) {
|
|
32
|
+
return `${ms.toFixed(2)} ms`.padStart(12);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function runOnce(command, args) {
|
|
36
|
+
const t0 = performance.now();
|
|
37
|
+
const result = spawnSync(command, args, { cwd: here, encoding: 'utf8' });
|
|
38
|
+
const wall = performance.now() - t0;
|
|
39
|
+
if (result.status !== 0) {
|
|
40
|
+
console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
const match = result.stdout.match(/computed in ([\d.]+) ms/);
|
|
44
|
+
if (!match) {
|
|
45
|
+
console.error(`unexpected output:\n${result.stdout}`);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
return { wall, compute: Number(match[1]) };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function measure(command, args) {
|
|
52
|
+
const walls = [];
|
|
53
|
+
const computes = [];
|
|
54
|
+
for (let i = 0; i < runs; i++) {
|
|
55
|
+
const sample = runOnce(command, args);
|
|
56
|
+
walls.push(sample.wall);
|
|
57
|
+
computes.push(sample.compute);
|
|
58
|
+
}
|
|
59
|
+
return { wall: stats(walls), compute: stats(computes) };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 1. Compile fibonacci to a native binary.
|
|
63
|
+
fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
|
|
64
|
+
console.log('Compiling main.ts to a native binary with TypeNative...');
|
|
65
|
+
const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
|
|
66
|
+
cwd: here,
|
|
67
|
+
stdio: 'inherit',
|
|
68
|
+
});
|
|
69
|
+
if (build.status !== 0) process.exit(build.status ?? 1);
|
|
70
|
+
|
|
71
|
+
// 2. Run fib(n) under both runtimes.
|
|
72
|
+
console.log(`\nComputing fib(${n}) ${runs} times per runtime...\n`);
|
|
73
|
+
const nodeStats = measure(process.execPath, ['main.ts', String(n)]);
|
|
74
|
+
const nativeStats = measure(exePath, [String(n)]);
|
|
75
|
+
|
|
76
|
+
console.log(`Compute time, fib(${n}) over ${runs} runs (measured in-program):\n`);
|
|
77
|
+
console.log('| Runtime | min | median | mean | max |');
|
|
78
|
+
console.log('|------------|--------------|--------------|--------------|--------------|');
|
|
79
|
+
console.log(`| Node.js |${fmt(nodeStats.compute.min)} |${fmt(nodeStats.compute.median)} |${fmt(nodeStats.compute.mean)} |${fmt(nodeStats.compute.max)} |`);
|
|
80
|
+
console.log(`| TypeNative |${fmt(nativeStats.compute.min)} |${fmt(nativeStats.compute.median)} |${fmt(nativeStats.compute.mean)} |${fmt(nativeStats.compute.max)} |`);
|
|
81
|
+
console.log(`\nTypeNative computes ${(nodeStats.compute.median / nativeStats.compute.median).toFixed(1)}x faster (median).\n`);
|
|
82
|
+
|
|
83
|
+
console.log(`Total wall time per run incl. startup (spawn -> exit):\n`);
|
|
84
|
+
console.log('| Runtime | min | median | mean | max |');
|
|
85
|
+
console.log('|------------|--------------|--------------|--------------|--------------|');
|
|
86
|
+
console.log(`| Node.js |${fmt(nodeStats.wall.min)} |${fmt(nodeStats.wall.median)} |${fmt(nodeStats.wall.mean)} |${fmt(nodeStats.wall.max)} |`);
|
|
87
|
+
console.log(`| TypeNative |${fmt(nativeStats.wall.min)} |${fmt(nativeStats.wall.median)} |${fmt(nativeStats.wall.mean)} |${fmt(nativeStats.wall.max)} |`);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Naive recursive Fibonacci: the classic CPU-bound benchmark. The same source
|
|
2
|
+
// runs unchanged under Node.js and as a compiled TypeNative binary, and times
|
|
3
|
+
// itself with performance.now() so both runtimes are measured the same way
|
|
4
|
+
// (compute time only, startup excluded).
|
|
5
|
+
//
|
|
6
|
+
// node main.ts [n] (default n = 35)
|
|
7
|
+
// ./fib [n]
|
|
8
|
+
import { performance } from 'node:perf_hooks';
|
|
9
|
+
|
|
10
|
+
function fib(n: number): number {
|
|
11
|
+
return n < 2 ? n : fib(n - 1) + fib(n - 2);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const n = Number(process.argv[2] ?? '35');
|
|
15
|
+
|
|
16
|
+
const start = performance.now();
|
|
17
|
+
const result = fib(n);
|
|
18
|
+
const elapsedMs = performance.now() - start;
|
|
19
|
+
|
|
20
|
+
console.log(`fib(${n}) = ${result}`);
|
|
21
|
+
console.log(`computed in ${elapsedMs.toFixed(2)} ms`);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Hello World
|
|
2
|
+
|
|
3
|
+
The smallest possible TypeNative program — a `console.log` with an optional
|
|
4
|
+
name from `process.argv`. It is the "hello world" of both TypeScript and
|
|
5
|
+
native compilation: no work is done at all, so benchmarking it measures pure
|
|
6
|
+
**runtime startup cost**.
|
|
7
|
+
|
|
8
|
+
## The code
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
// main.ts
|
|
12
|
+
const name = process.argv[2] ?? 'world';
|
|
13
|
+
console.log(`Hello, ${name}!`);
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Run it
|
|
17
|
+
|
|
18
|
+
With Node.js:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
node main.ts Ada
|
|
22
|
+
# Hello, Ada!
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
With TypeNative (compile to a native binary and run it):
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npx typenative --source main.ts --output dist/hello.exe
|
|
29
|
+
./dist/hello.exe Ada
|
|
30
|
+
# Hello, Ada!
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
From a checkout of this repository, use the local CLI instead of `npx`:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
node bin/cli.js --source examples/hello-world/main.ts --script # compile + run
|
|
37
|
+
node bin/cli.js --source examples/hello-world/main.ts --output dist/hello.exe
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
(`--script` compiles and runs in one step; passing program arguments requires a
|
|
41
|
+
built binary, as shown above.)
|
|
42
|
+
|
|
43
|
+
## Benchmark: Node.js vs TypeNative
|
|
44
|
+
|
|
45
|
+
Reproduce with `node bench.mjs [runs=30]`. The script compiles `main.ts` with
|
|
46
|
+
the TypeNative CLI, then times **complete process runs** (spawn to exit) of
|
|
47
|
+
the same program under both runtimes. Since the program does no work, the
|
|
48
|
+
numbers are almost entirely startup time.
|
|
49
|
+
|
|
50
|
+
**Environment:** Windows 11 Pro, AMD Ryzen 5 5500U, Node v24.19.0, Go 1.26.0,
|
|
51
|
+
TypeNative 0.4.0, default (non-`--release`) build.
|
|
52
|
+
|
|
53
|
+
| Runtime | min | median | mean | max |
|
|
54
|
+
|------------|------------|------------|------------|------------|
|
|
55
|
+
| Node.js | 97.07 ms | 103.45 ms | 104.51 ms | 125.91 ms |
|
|
56
|
+
| TypeNative | 12.95 ms | 14.72 ms | 15.18 ms | 25.11 ms |
|
|
57
|
+
|
|
58
|
+
**TypeNative starts 7.0x faster** (median). The compiled binary is a
|
|
59
|
+
self-contained 2.4 MB executable: there is no runtime to boot, no module
|
|
60
|
+
system to initialize, and no JIT to warm up, so process-to-output latency is
|
|
61
|
+
an order of magnitude lower. For CLI tools that are invoked frequently, this
|
|
62
|
+
is the difference users notice.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Startup benchmark: Node.js vs TypeNative for the hello-world program.
|
|
2
|
+
//
|
|
3
|
+
// Compiles main.ts to a native binary with the TypeNative CLI from this repo
|
|
4
|
+
// checkout (../../bin/cli.js), then times complete process runs (spawn to
|
|
5
|
+
// exit) of the same program under both runtimes. Because the program itself
|
|
6
|
+
// does almost no work, this mostly measures runtime startup cost.
|
|
7
|
+
//
|
|
8
|
+
// Usage: node bench.mjs [runs=30]
|
|
9
|
+
|
|
10
|
+
import { spawnSync } from 'node:child_process';
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
|
|
15
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const repoRoot = path.resolve(here, '..', '..');
|
|
17
|
+
const cli = path.join(repoRoot, 'bin', 'cli.js');
|
|
18
|
+
const runs = Number(process.argv[2] ?? '30') || 30;
|
|
19
|
+
const exeName = process.platform === 'win32' ? 'hello.exe' : 'hello';
|
|
20
|
+
const exePath = path.join(here, 'dist', exeName);
|
|
21
|
+
|
|
22
|
+
function stats(samples) {
|
|
23
|
+
const sorted = [...samples].sort((a, b) => a - b);
|
|
24
|
+
const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
|
|
25
|
+
return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function fmt(ms) {
|
|
29
|
+
return `${ms.toFixed(2)} ms`.padStart(11);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function timeRuns(command, args) {
|
|
33
|
+
const samples = [];
|
|
34
|
+
for (let i = 0; i < runs; i++) {
|
|
35
|
+
const t0 = performance.now();
|
|
36
|
+
const result = spawnSync(command, args, { cwd: here, encoding: 'utf8' });
|
|
37
|
+
const elapsed = performance.now() - t0;
|
|
38
|
+
if (result.status !== 0) {
|
|
39
|
+
console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
samples.push(elapsed);
|
|
43
|
+
}
|
|
44
|
+
return stats(samples);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 1. Compile hello-world to a native binary.
|
|
48
|
+
fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
|
|
49
|
+
console.log('Compiling main.ts to a native binary with TypeNative...');
|
|
50
|
+
const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
|
|
51
|
+
cwd: here,
|
|
52
|
+
stdio: 'inherit',
|
|
53
|
+
});
|
|
54
|
+
if (build.status !== 0) process.exit(build.status ?? 1);
|
|
55
|
+
|
|
56
|
+
// 2. Time full process runs under both runtimes.
|
|
57
|
+
console.log(`\nTiming ${runs} full process runs of each runtime...\n`);
|
|
58
|
+
const nodeStats = timeRuns(process.execPath, ['main.ts']);
|
|
59
|
+
const nativeStats = timeRuns(exePath, []);
|
|
60
|
+
|
|
61
|
+
console.log(`Startup time, ${runs} runs (full process: spawn -> exit):\n`);
|
|
62
|
+
console.log('| Runtime | min | median | mean | max |');
|
|
63
|
+
console.log('|------------|-------------|-------------|-------------|-------------|');
|
|
64
|
+
console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |`);
|
|
65
|
+
console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |`);
|
|
66
|
+
console.log(`\nTypeNative starts ${(nodeStats.median / nativeStats.median).toFixed(1)}x faster (median).`);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// The smallest possible TypeNative program: print a greeting to stdout.
|
|
2
|
+
// An optional name can be passed as the first CLI argument:
|
|
3
|
+
//
|
|
4
|
+
// node main.ts Ada -> Hello, Ada!
|
|
5
|
+
// ./hello Ada -> Hello, Ada!
|
|
6
|
+
const name = process.argv[2] ?? 'world';
|
|
7
|
+
console.log(`Hello, ${name}!`);
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# JSON Server
|
|
2
|
+
|
|
3
|
+
A tiny JSON API for managing todos, built with nothing but the Node standard
|
|
4
|
+
library — `node:http`, `JSON`, and `URL`. The todos live in memory, so the
|
|
5
|
+
same file is a complete, dependency-free web server under Node.js and as a
|
|
6
|
+
compiled TypeNative binary.
|
|
7
|
+
|
|
8
|
+
## The code
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
// main.ts (abridged — see main.ts for the full server)
|
|
12
|
+
import http from 'node:http';
|
|
13
|
+
|
|
14
|
+
interface Todo {
|
|
15
|
+
id: number;
|
|
16
|
+
title: string;
|
|
17
|
+
done: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const todos: Todo[] = [];
|
|
21
|
+
let nextId = 1;
|
|
22
|
+
|
|
23
|
+
const server = http.createServer((req, res) => {
|
|
24
|
+
let body = '';
|
|
25
|
+
req.on('data', (chunk) => { body += String(chunk); });
|
|
26
|
+
req.on('end', () => {
|
|
27
|
+
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
28
|
+
|
|
29
|
+
if (req.method === 'GET' && url.pathname === '/todos') {
|
|
30
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
31
|
+
res.end(JSON.stringify(todos));
|
|
32
|
+
} else if (req.method === 'POST' && url.pathname === '/todos') {
|
|
33
|
+
const input = JSON.parse(body) as { title: string };
|
|
34
|
+
// ... validate, create, respond 201 ...
|
|
35
|
+
} else {
|
|
36
|
+
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
37
|
+
res.end(JSON.stringify({ error: 'no route' }));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
server.listen(3000, '127.0.0.1');
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Routes
|
|
46
|
+
|
|
47
|
+
| Route | Method | Description |
|
|
48
|
+
|-----------------|--------|----------------------------------------------|
|
|
49
|
+
| `/todos` | GET | List all todos |
|
|
50
|
+
| `/todos/<id>` | GET | Fetch one todo (404 if missing) |
|
|
51
|
+
| `/todos` | POST | Create a todo: `{ "title": "..." }` → 201 |
|
|
52
|
+
| anything else | * | `404` with a JSON error |
|
|
53
|
+
|
|
54
|
+
A missing `title` returns `400` with a JSON error. (The example keeps
|
|
55
|
+
`JSON.parse` unguarded, so invalid JSON throws — under Node and the compiled
|
|
56
|
+
binary alike, an uncaught throw ends the process.)
|
|
57
|
+
|
|
58
|
+
## Run it
|
|
59
|
+
|
|
60
|
+
With Node.js:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
node main.ts # listens on http://127.0.0.1:3000
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
With TypeNative (compile to a single self-contained binary — no Node
|
|
67
|
+
required on the machine that runs it):
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
npx typenative --source main.ts --output dist/server.exe
|
|
71
|
+
./dist/server.exe # optional port argument or PORT env var
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
From a checkout of this repository, use the local CLI instead of `npx`:
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
node bin/cli.js --source examples/json-server/main.ts --script
|
|
78
|
+
node bin/cli.js --source examples/json-server/main.ts --output dist/server.exe
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Try it:
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
curl -s http://127.0.0.1:3000/todos # []
|
|
85
|
+
curl -s -X POST -d '{"title":"write examples"}' \
|
|
86
|
+
http://127.0.0.1:3000/todos # {"id":1,...}
|
|
87
|
+
curl -s http://127.0.0.1:3000/todos/1 # one todo
|
|
88
|
+
curl -s -w "%{http_code}" -X POST -d '{}' http://127.0.0.1:3000/todos # 400
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Benchmark: Node.js vs TypeNative
|
|
92
|
+
|
|
93
|
+
Reproduce with `node bench.mjs [gets=20000] [posts=10000]`. The script
|
|
94
|
+
compiles `main.ts` with the TypeNative CLI, starts the server under each
|
|
95
|
+
runtime, seeds 50 todos, and drives load over keep-alive connections at
|
|
96
|
+
concurrency 16 (identical client for both). It measures requests/second plus
|
|
97
|
+
p50/p99 latency per request for `GET /todos` (20,000 requests, ~50-item JSON
|
|
98
|
+
response) and `POST /todos` (10,000 requests).
|
|
99
|
+
|
|
100
|
+
The load generator is built to stay out of the way, like `wrk` or
|
|
101
|
+
`autocannon`: 8 worker threads × 2 raw keep-alive sockets send prebuilt
|
|
102
|
+
request bytes and read each response just far enough to find its end. A
|
|
103
|
+
plain `node:http` client tops out around 8–11k req/s on this machine, so
|
|
104
|
+
with it both servers would simply tie at the client's limit.
|
|
105
|
+
|
|
106
|
+
**Environment:** Windows 11 Pro, AMD Ryzen 5 5500U, Node v24.19.0, Go 1.26.0,
|
|
107
|
+
TypeNative 0.4.0, default (non-`--release`) build, client and server on the
|
|
108
|
+
same machine. Re-run variation is about ±10%; across runs TypeNative was
|
|
109
|
+
1.2–1.3x Node on GET and 1.2–1.4x on POST.
|
|
110
|
+
|
|
111
|
+
| Runtime | Endpoint | req/s | p50 (ms) | p99 (ms) |
|
|
112
|
+
|------------|-------------|-------:|---------:|---------:|
|
|
113
|
+
| Node.js | GET /todos | 13379 | 1.02 | 3.03 |
|
|
114
|
+
| TypeNative | GET /todos | 17794 | 0.66 | 1.87 |
|
|
115
|
+
| Node.js | POST /todos | 15405 | 0.94 | 2.17 |
|
|
116
|
+
| TypeNative | POST /todos | 19581 | 0.60 | 2.32 |
|
|
117
|
+
|
|
118
|
+
**TypeNative serves ~1.3x more requests per second**, with lower median
|
|
119
|
+
latency on both routes. JavaScript still runs one thing at a time, as in
|
|
120
|
+
Node, but the compiled server spreads everything around it — socket reads
|
|
121
|
+
and writes, HTTP parsing — across Go's `net/http` goroutines, and runs the
|
|
122
|
+
handler right on the request's goroutine whenever the event loop is free.
|
|
123
|
+
|
|
124
|
+
On top of that, the binary starts in ~15 ms instead of ~100 ms and is a
|
|
125
|
+
single 9.7 MB executable with zero dependencies — `scp` it to a server and
|
|
126
|
+
run it, no Node install, no `node_modules` — and CPU-heavy work inside a
|
|
127
|
+
handler runs ~2x faster (see the [fibonacci example](../fibonacci/README.md)).
|