typenative 0.3.1 → 0.4.0
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/CHANGELOG.md +57 -0
- package/README.md +17 -1
- package/bin/cli.js +25 -11
- package/bin/go/convert.js +11 -1
- package/bin/index.js +221 -47
- package/bin/main.js +35 -14
- package/bin/node/assert.js +2 -2
- package/bin/node/buffer.js +9 -0
- package/bin/node/dns.js +1 -1
- package/bin/node/events.js +1 -1
- package/bin/node/fs.js +1 -1
- package/bin/node/fs_watch.js +407 -0
- package/bin/node/http2.js +33 -6
- package/bin/node/readline.js +1 -1
- package/bin/node/registry.js +2 -1
- package/bin/node/stream.js +6 -3
- package/bin/node/timers.js +1 -1
- package/bin/node/typed_arrays.js +608 -0
- package/bin/node/util.js +3 -0
- package/bin/node/zlib.js +131 -16
- package/bin/transpiler.js +635 -44
- package/bin/version.js +1 -1
- package/conformance.md +16 -4
- 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 +125 -0
- package/examples/json-server/bench.mjs +171 -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-npm.d.ts +72 -0
- package/types/typenative.d.ts +117 -1
package/bin/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by scripts/gen-version.mjs — do not edit.
|
|
2
|
-
export const VERSION = '0.
|
|
2
|
+
export const VERSION = '0.4.0';
|
package/conformance.md
CHANGED
|
@@ -246,6 +246,16 @@ self-hosted binary). Each ❌ row is a known gap with no passing probe yet.
|
|
|
246
246
|
| Loop closures capture per-iteration `let` | ✅ | [semantics-loop-closures.ts](conformance/semantics-loop-closures.ts) | |
|
|
247
247
|
| Functions stored in Records | ✅ | [semantics-record-functions.ts](conformance/semantics-record-functions.ts) | `rec.fn()` → `rec.Get("fn")()`, also for names like `get` / `sort` |
|
|
248
248
|
|
|
249
|
+
## Sample apps
|
|
250
|
+
|
|
251
|
+
End-to-end programs that combine many features the way real code does (they run in the suite like any probe).
|
|
252
|
+
|
|
253
|
+
| App | Probe | Covers |
|
|
254
|
+
| --- | ----- | ------ |
|
|
255
|
+
| `wc`-style CLI | [app-cli-wordcount.ts](conformance/app-cli-wordcount.ts) | `util.parseArgs`, `fs`, `path`, `os.EOL`, a class with a `Map` field, tuples sorted with a `\|\|` comparator, `reduce` into an object, `crypto` |
|
|
256
|
+
| JSON API on HTTP + SQLite | [app-http-sqlite.ts](conformance/app-http-sqlite.ts) | `http.createServer` routing, `URL`, `node:sqlite` prepared statements, `fetch` (POST / GET / DELETE), JSON bodies, status codes |
|
|
257
|
+
| Stream pipeline | [app-stream-pipeline.ts](conformance/app-stream-pipeline.ts) | A generator through `Readable.from`, a CSV-aggregating `Transform`, `Float64Array` series, `stream/promises` `pipeline` to files, gzip and Brotli streams, SHA-256 |
|
|
258
|
+
|
|
249
259
|
## Standard library
|
|
250
260
|
|
|
251
261
|
Every standard-library API TypeNative ports, split by module. Per
|
|
@@ -514,7 +524,7 @@ async function runs synchronously until its first `await`.
|
|
|
514
524
|
| `read*` / `write*` (U)Int8/16/32 LE/BE, Float, Double | ✅ | [lib-node-buffer-binary.ts](conformance/lib-node-buffer-binary.ts) | Out-of-range offsets throw `ERR_OUT_OF_RANGE` |
|
|
515
525
|
| `Blob` / `File` (`size`, `type`, `name`, `lastModified`, `text()` / `bytes()` / `arrayBuffer()`, `slice`, `stream()`); parts of strings, Buffers and Blobs | ✅ | [lib-node-blob.ts](conformance/lib-node-blob.ts) | Also globals; bytes held in memory |
|
|
516
526
|
| `read/writeBigInt64LE/BE`, `read/writeBigUInt64LE/BE`, `swap16/32/64`; `isUtf8` / `isAscii` / `transcode` / `kMaxLength` | ✅ | [lib-node-buffer-bigint.ts](conformance/lib-node-buffer-bigint.ts) | bigints are `*big.Int` |
|
|
517
|
-
| Typed arrays
|
|
527
|
+
| Typed arrays (`Int8Array`, `Uint8ClampedArray`, `Int16Array`, `Uint16Array`, `Int32Array`, `Uint32Array`, `Float32Array`, `Float64Array`): indexing, `length` / `byteLength` / `buffer`, `from` / `of`, `fill` / `set` / `subarray` / `slice` / `copyWithin`, `map` / `filter` / `reduce` / `forEach` & co., `sort` / `toSorted`, `join`, `for…of`, spread; `ArrayBuffer` (`slice`, `isView`); `DataView` (all getters / setters incl. BigInt64) | ✅ | [lib-typed-arrays.ts](conformance/lib-typed-arrays.ts) | One Go slice per element type; views on an `ArrayBuffer` share its memory. Writes convert like JS (wrapping, clamping). A view's `.buffer` is its own bytes (`byteOffset` 0). Out-of-range reads give `0`, not `undefined`. `BigInt64Array` / `BigUint64Array` are not implemented |
|
|
518
528
|
|
|
519
529
|
#### `node:fs`
|
|
520
530
|
|
|
@@ -529,7 +539,7 @@ async function runs synchronously until its first `await`.
|
|
|
529
539
|
| `fs/promises` / `fs.promises` (incl. `FileHandle` from `open`) | ✅ | [lib-node-fs-promises.ts](conformance/lib-node-fs-promises.ts) | Rejections carry the error code |
|
|
530
540
|
| `createReadStream` / `createWriteStream` | ✅ | [lib-node-fs-streams.ts](conformance/lib-node-fs-streams.ts) | `encoding`, `start` / `end`, `highWaterMark`, `flags` |
|
|
531
541
|
| `chmodSync` / `utimesSync` / `symlinkSync` / `readlinkSync` / `linkSync` | ✅ | — | Implemented, not probed (symlinks need privileges on Windows) |
|
|
532
|
-
| `watch` / `watchFile`
|
|
542
|
+
| `watch` (`recursive`, `persistent`, `signal`, `ref` / `unref`, `'change'` / `'error'` / `'close'`) / `watchFile` / `unwatchFile`; `fs/promises` `watch` | ✅ | [lib-node-fs-watch.ts](conformance/lib-node-fs-watch.ts) | `watch` uses the OS's notifications through the vendored `fsnotify` (only linked into programs that call it); recursive watches add subdirectories as they appear. `watchFile` polls `stat()` |
|
|
533
543
|
|
|
534
544
|
#### `node:os`
|
|
535
545
|
|
|
@@ -630,7 +640,7 @@ async function runs synchronously until its first `await`.
|
|
|
630
640
|
| `gzipSync` / `gunzipSync` / `deflateSync` / `inflateSync` / `deflateRawSync` / `inflateRawSync` / `unzipSync` / `crc32` / `constants` | ✅ | [lib-node-zlib.ts](conformance/lib-node-zlib.ts) | `{ level }`; invalid input throws `Z_DATA_ERROR` |
|
|
631
641
|
| `createGzip` / `createGunzip` / `createDeflate` / `createInflate` / `createDeflateRaw` / `createInflateRaw` / `createUnzip` | ✅ | [lib-node-zlib-streams.ts](conformance/lib-node-zlib-streams.ts) | Transforms that (de)compress when the input ends |
|
|
632
642
|
| Callback forms `gzip` / `gunzip` / `deflate` / `inflate` / `deflateRaw` / `inflateRaw` / `unzip` (`buf, [options], cb`) and `util.promisify` of them | ✅ | [lib-node-zlib-async.ts](conformance/lib-node-zlib-async.ts) | The codec runs on a goroutine; the callback runs on the event loop |
|
|
633
|
-
| Brotli
|
|
643
|
+
| Brotli: `brotliCompressSync` / `brotliDecompressSync`, callback and promisified `brotliCompress` / `brotliDecompress`, `createBrotliCompress` / `createBrotliDecompress`, `constants.BROTLI_*` | ✅ | [lib-node-zlib-brotli.ts](conformance/lib-node-zlib-brotli.ts) | The vendored `andybalholm/brotli` (pure Go, only linked into programs that use it); `params` quality / window. Output matches Node's; decoder errors carry Node's codes |
|
|
634
644
|
|
|
635
645
|
#### `node:timers`
|
|
636
646
|
|
|
@@ -934,7 +944,7 @@ Status: ✅ probed, ⚠️ typed (declared and bridged), no probe yet, ❌ exclu
|
|
|
934
944
|
| `go:compress/gzip` | ✅ | [lib-go-compress-gzip.ts](conformance/lib-go-compress-gzip.ts) | gzip read/write |
|
|
935
945
|
| `go:compress/lzw` | ✅ | [lib-go-compress-extra.ts](conformance/lib-go-compress-extra.ts) | LZW compression (GIF/PDF/TIFF) |
|
|
936
946
|
| `go:compress/zlib` | ✅ | [lib-go-compress-extra.ts](conformance/lib-go-compress-extra.ts) | zlib read/write |
|
|
937
|
-
| `go:container/heap` |
|
|
947
|
+
| `go:container/heap` | ✅ | [lib-go-container-heap.ts](conformance/lib-go-container-heap.ts) | A TS class implementing `heap.Interface` (see Go interfaces below) |
|
|
938
948
|
| `go:container/list` | ✅ | [lib-go-containers.ts](conformance/lib-go-containers.ts) | Doubly linked list |
|
|
939
949
|
| `go:container/ring` | ✅ | [lib-go-containers.ts](conformance/lib-go-containers.ts) | Circular list |
|
|
940
950
|
| `go:context` | ✅ | [lib-go-sync.ts](conformance/lib-go-sync.ts) | Cancellation and deadlines |
|
|
@@ -1119,9 +1129,11 @@ Status: ✅ probed, ⚠️ typed (declared and bridged), no probe yet, ❌ exclu
|
|
|
1119
1129
|
| Out-params | `ref<T>(init?)` makes a pointer cell; `.value` dereferences it. |
|
|
1120
1130
|
| Byte slices | `[]byte` is `Uint8Array` (zero-copy); `number[]` converts per call (mutations of the copy are lost). |
|
|
1121
1131
|
| Side-effect imports | `import 'go:time/tzdata'` emits a blank `import _`. |
|
|
1132
|
+
| Go interfaces | A TS class instance passed where Go expects an interface (`heap.Interface`, `sort.Interface`) is wrapped in a generated adapter with the exact Go method signatures (`Len() int` calls the class's `Len(): number`); the class needs every method, inherited ones count. Values typed as the interface itself are not adapted. |
|
|
1122
1133
|
|
|
1123
1134
|
### npm packages
|
|
1124
1135
|
|
|
1125
1136
|
| Pattern | Status | Probe | Notes |
|
|
1126
1137
|
| ------------------------- | :----: | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
|
|
1127
1138
|
| `import { x } from 'pkg'` | ✅ | [syntax-import-npm.ts](conformance/syntax-import-npm.ts) | Mapped to Go module imports (fixture dep in [conformance/package.json](conformance/package.json)) |
|
|
1139
|
+
| Packages published as plain JavaScript / CommonJS (tried: `ms`, `escape-html`, `tiny-invariant`) | ❌ | — | Without TypeScript sources there are no types to compile from: untyped packages stop at the type check (TS7016), and typed ones whose code is CommonJS (`module.exports`, union-typed callbacks) don't lower. Packages that ship `.ts` sources work |
|
|
@@ -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 | Node **1.5x higher HTTP throughput** (honest!) |
|
|
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
|
+
The json-server harness also shows where Node is still ahead: its V8 +
|
|
52
|
+
`node:http` stack out-serves the compiled binary ~1.5x on I/O-bound HTTP
|
|
53
|
+
throughput, while TypeNative wins process startup (~7x) and CPU-bound
|
|
54
|
+
compute (~2x). The READMEs report what was measured, not what flatters.
|
|
@@ -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,125 @@
|
|
|
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
|
+
Invalid JSON or a missing `title` returns `400` with a JSON error.
|
|
55
|
+
|
|
56
|
+
## Run it
|
|
57
|
+
|
|
58
|
+
With Node.js:
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
node main.ts # listens on http://127.0.0.1:3000
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
With TypeNative (compile to a single self-contained binary — no Node
|
|
65
|
+
required on the machine that runs it):
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
npx typenative --source main.ts --output dist/server.exe
|
|
69
|
+
./dist/server.exe # optional port argument or PORT env var
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
From a checkout of this repository, use the local CLI instead of `npx`:
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
node bin/cli.js --source examples/json-server/main.ts --script
|
|
76
|
+
node bin/cli.js --source examples/json-server/main.ts --output dist/server.exe
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Try it:
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
curl -s http://127.0.0.1:3000/todos # []
|
|
83
|
+
curl -s -X POST -d '{"title":"write examples"}' \
|
|
84
|
+
http://127.0.0.1:3000/todos # {"id":1,...}
|
|
85
|
+
curl -s http://127.0.0.1:3000/todos/1 # one todo
|
|
86
|
+
curl -s -w "%{http_code}" -X POST -d '{}' http://127.0.0.1:3000/todos # 400
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Benchmark: Node.js vs TypeNative
|
|
90
|
+
|
|
91
|
+
Reproduce with `node bench.mjs [gets=20000] [posts=10000]`. The script
|
|
92
|
+
compiles `main.ts` with the TypeNative CLI, starts the server under each
|
|
93
|
+
runtime, seeds 50 todos, and drives load over keep-alive connections at
|
|
94
|
+
concurrency 16 with a Node.js `node:http` client (identical client for both).
|
|
95
|
+
It measures requests/second plus p50/p99 latency per request for
|
|
96
|
+
`GET /todos` (20,000 requests, ~50-item JSON response) and `POST /todos`
|
|
97
|
+
(10,000 requests).
|
|
98
|
+
|
|
99
|
+
**Environment:** Windows 11 Pro, AMD Ryzen 5 5500U, Node v24.19.0, Go 1.26.0,
|
|
100
|
+
TypeNative 0.4.0, default (non-`--release`) build. Re-run variation is about
|
|
101
|
+
±5%.
|
|
102
|
+
|
|
103
|
+
| Runtime | Endpoint | req/s | p50 (ms) | p99 (ms) |
|
|
104
|
+
|------------|-------------|-------:|---------:|---------:|
|
|
105
|
+
| Node.js | GET /todos | 7709 | 1.87 | 6.05 |
|
|
106
|
+
| TypeNative | GET /todos | 4834 | 3.08 | 6.60 |
|
|
107
|
+
| Node.js | POST /todos | 9244 | 1.57 | 3.97 |
|
|
108
|
+
| TypeNative | POST /todos | 6141 | 2.46 | 4.55 |
|
|
109
|
+
|
|
110
|
+
**Node.js serves ~1.5x more requests per second** on this workload. That is
|
|
111
|
+
the honest result, and it is worth understanding: V8 and Node's `node:http`
|
|
112
|
+
have been tuned for exactly this I/O-bound pattern for over a decade, while
|
|
113
|
+
TypeNative re-implements the request lifecycle (event emission, stream
|
|
114
|
+
callbacks, per-request closures) on top of Go's `net/http`, and that
|
|
115
|
+
translation still has overhead. Tail latency (p99) is comparable for both.
|
|
116
|
+
|
|
117
|
+
Where the compiled binary wins is everything *around* the steady-state loop:
|
|
118
|
+
it starts in ~15 ms instead of ~100 ms, it is a single 9.7 MB executable with
|
|
119
|
+
zero dependencies — `scp` it to a server and run it, no Node install, no
|
|
120
|
+
`node_modules` — and CPU-heavy work inside a handler runs ~2x faster (see the
|
|
121
|
+
[fibonacci example](../fibonacci/README.md)).
|
|
122
|
+
|
|
123
|
+
Takeaway: for I/O-bound HTTP services Node remains the throughput king today;
|
|
124
|
+
choose TypeNative when startup time, deployment simplicity, or CPU-bound
|
|
125
|
+
handler work dominates.
|