typenative 0.4.1 → 0.5.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.
Files changed (151) hide show
  1. package/CHANGELOG.md +171 -0
  2. package/README.md +62 -22
  3. package/ROUND2-HANDOFF.md +200 -0
  4. package/bin/cli.js +1 -0
  5. package/bin/index.js +13 -2
  6. package/bin/main.js +1 -0
  7. package/bin/node/assert.js +2 -2
  8. package/bin/node/async_hooks.js +1 -1
  9. package/bin/node/buffer.js +16 -5
  10. package/bin/node/child_process.js +7 -7
  11. package/bin/node/cluster.js +3 -3
  12. package/bin/node/crypto_keys.js +21 -21
  13. package/bin/node/dgram.js +1 -1
  14. package/bin/node/diagnostics_channel.js +3 -3
  15. package/bin/node/dns.js +2 -2
  16. package/bin/node/events.js +3 -3
  17. package/bin/node/fetch.js +2 -2
  18. package/bin/node/fs.js +3 -3
  19. package/bin/node/fs_watch.js +5 -5
  20. package/bin/node/http.js +3 -3
  21. package/bin/node/http2.js +3 -3
  22. package/bin/node/net.js +8 -8
  23. package/bin/node/path.js +17 -3
  24. package/bin/node/process.js +34 -11
  25. package/bin/node/readline.js +1 -1
  26. package/bin/node/registry.js +4 -1
  27. package/bin/node/sqlite.js +6 -6
  28. package/bin/node/stream.js +81 -15
  29. package/bin/node/stream_web.js +10 -10
  30. package/bin/node/system_extra.js +3 -3
  31. package/bin/node/test.js +674 -366
  32. package/bin/node/test_coverage.js +187 -0
  33. package/bin/node/test_report.js +1392 -0
  34. package/bin/node/test_run.js +575 -0
  35. package/bin/node/timers.js +4 -4
  36. package/bin/node/tls.js +5 -5
  37. package/bin/node/util.js +31 -7
  38. package/bin/node/util_extra.js +7 -7
  39. package/bin/node/webview.js +654 -0
  40. package/bin/node/worker_threads.js +1 -1
  41. package/bin/node/zlib.js +3 -3
  42. package/bin/parse-node.js +4 -0
  43. package/bin/transpiler.js +10390 -1178
  44. package/bin/version.js +1 -1
  45. package/conformance.md +468 -340
  46. package/examples/README.md +47 -10
  47. package/examples/desktop-notes/README.md +108 -0
  48. package/examples/desktop-notes/bench.mjs +125 -0
  49. package/examples/desktop-notes/main.ts +131 -0
  50. package/examples/desktop-notes/package-lock.json +726 -0
  51. package/examples/desktop-notes/package.json +9 -0
  52. package/examples/desktop-notes/ui.ts +176 -0
  53. package/examples/dupe-finder/README.md +76 -0
  54. package/examples/dupe-finder/bench.mjs +120 -0
  55. package/examples/dupe-finder/main.ts +87 -0
  56. package/examples/mandelbrot-png/README.md +65 -0
  57. package/examples/mandelbrot-png/bench.mjs +95 -0
  58. package/examples/mandelbrot-png/main.ts +98 -0
  59. package/examples/mandelbrot-png/preview.png +0 -0
  60. package/examples/marked-cli/README.md +159 -0
  61. package/examples/marked-cli/bench.mjs +178 -0
  62. package/examples/marked-cli/main.ts +292 -0
  63. package/examples/marked-cli/parity.mjs +185 -0
  64. package/examples/marked-cli/vendor/marked/LICENSE +44 -0
  65. package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
  66. package/examples/todo-cli/README.md +69 -0
  67. package/examples/todo-cli/bench.mjs +89 -0
  68. package/examples/todo-cli/main.ts +92 -0
  69. package/examples/yaml-cli/README.md +136 -0
  70. package/examples/yaml-cli/bench.mjs +265 -0
  71. package/examples/yaml-cli/main.ts +29 -0
  72. package/examples/yaml-cli/parity.mjs +156 -0
  73. package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
  74. package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
  75. package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
  76. package/govendor/github.com/jchv/go-webview2/README.md +21 -0
  77. package/govendor/github.com/jchv/go-webview2/common.go +84 -0
  78. package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
  79. package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
  80. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
  81. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
  82. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
  83. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
  84. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
  85. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
  86. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
  87. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
  88. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
  89. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
  90. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
  91. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
  92. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
  93. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
  94. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
  95. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
  96. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
  97. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
  98. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
  99. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
  100. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
  101. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
  102. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
  103. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
  104. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
  105. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
  106. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
  107. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
  108. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
  109. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
  110. package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
  111. package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
  112. package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
  113. package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
  114. package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
  115. package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
  116. package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
  117. package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
  118. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
  119. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
  120. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
  121. package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
  122. package/govendor/github.com/jchv/go-winloader/README.md +124 -0
  123. package/govendor/github.com/jchv/go-winloader/common.go +11 -0
  124. package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
  125. package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
  126. package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
  127. package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
  128. package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
  129. package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
  130. package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
  131. package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
  132. package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
  133. package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
  134. package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
  135. package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
  136. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
  137. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
  138. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
  139. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
  140. package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
  141. package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
  142. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
  143. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
  144. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
  145. package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
  146. package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
  147. package/govendor/manifest.txt +3 -1
  148. package/package.json +1 -1
  149. package/tsparser/main.go +4 -0
  150. package/types/typenative-npm.d.ts +190 -2
  151. package/types/typenative.d.ts +98 -31
@@ -0,0 +1,89 @@
1
+ // Per-command latency benchmark: Node.js vs TypeNative for the todo CLI.
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 `todo list` and `todo add` against a 50-item list in a temp file.
6
+ // Each command does a little file I/O and JSON work, so this measures what a
7
+ // user waits for on every invocation: startup plus a small amount of real work.
8
+ //
9
+ // Usage: node bench.mjs [runs=30]
10
+
11
+ import { spawnSync } from 'node:child_process';
12
+ import fs from 'node:fs';
13
+ import os from 'node:os';
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 runs = Number(process.argv[2] ?? '30') || 30;
21
+ const exeName = process.platform === 'win32' ? 'todo.exe' : 'todo';
22
+ const exePath = path.join(here, 'dist', exeName);
23
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'todo-bench-'));
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(11);
33
+ }
34
+
35
+ // A fresh 50-item list for each runtime, so `add` runs grow both the same way
36
+ function seed(file) {
37
+ const items = Array.from({ length: 50 }, (_, i) => ({
38
+ id: i + 1,
39
+ text: `Task number ${i + 1}`,
40
+ done: i % 3 === 0,
41
+ created: new Date(0).toISOString(),
42
+ }));
43
+ fs.writeFileSync(file, JSON.stringify({ nextId: 51, items }, null, 2));
44
+ }
45
+
46
+ function timeRuns(command, args, file) {
47
+ const samples = [];
48
+ for (let i = 0; i < runs; i++) {
49
+ const t0 = performance.now();
50
+ const result = spawnSync(command, args, { cwd: here, encoding: 'utf8', env: { ...process.env, TODO_FILE: file } });
51
+ const elapsed = performance.now() - t0;
52
+ if (result.status !== 0) {
53
+ console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
54
+ process.exit(1);
55
+ }
56
+ samples.push(elapsed);
57
+ }
58
+ return stats(samples);
59
+ }
60
+
61
+ // 1. Compile the CLI to a native binary.
62
+ fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
63
+ console.log('Compiling main.ts to a native binary with TypeNative...');
64
+ const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
65
+ cwd: here,
66
+ stdio: 'inherit',
67
+ });
68
+ if (build.status !== 0) process.exit(build.status ?? 1);
69
+
70
+ // 2. Time `list` and `add` under both runtimes, each on its own list file.
71
+ const nodeFile = path.join(tmpDir, 'node.json');
72
+ const nativeFile = path.join(tmpDir, 'native.json');
73
+ seed(nodeFile);
74
+ seed(nativeFile);
75
+ console.log(`\nTiming ${runs} full process runs of each command...\n`);
76
+ const results = [
77
+ ['list', timeRuns(process.execPath, ['main.ts', 'list'], nodeFile), timeRuns(exePath, ['list'], nativeFile)],
78
+ ['add', timeRuns(process.execPath, ['main.ts', 'add', 'Bench item'], nodeFile), timeRuns(exePath, ['add', 'Bench item'], nativeFile)],
79
+ ];
80
+ fs.rmSync(tmpDir, { recursive: true, force: true });
81
+
82
+ for (const [command, nodeStats, nativeStats] of results) {
83
+ console.log(`\`todo ${command}\`, ${runs} runs (full process: spawn -> exit):\n`);
84
+ console.log('| Runtime | min | median | mean | max |');
85
+ console.log('|------------|-------------|-------------|-------------|-------------|');
86
+ console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |`);
87
+ console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |`);
88
+ console.log(`\nTypeNative is ${(nodeStats.median / nativeStats.median).toFixed(1)}x faster per call (median).\n`);
89
+ }
@@ -0,0 +1,92 @@
1
+ // A to-do list for the terminal, kept in a JSON file. Commands are meant to be
2
+ // run many times a day (or from shell hooks), so process startup dominates
3
+ // every call: that is where a native binary pays off.
4
+ //
5
+ // todo add Buy milk add an item
6
+ // todo list show open and done items
7
+ // todo done 2 mark item 2 as done
8
+ // todo rm 2 delete item 2
9
+ //
10
+ // Items live in ~/.todo.json; set TODO_FILE to use another file.
11
+ import fs from 'node:fs';
12
+ import os from 'node:os';
13
+ import path from 'node:path';
14
+
15
+ interface Item {
16
+ id: number;
17
+ text: string;
18
+ done: boolean;
19
+ created: string;
20
+ }
21
+
22
+ interface Store {
23
+ nextId: number;
24
+ items: Item[];
25
+ }
26
+
27
+ const file = process.env.TODO_FILE ?? path.join(os.homedir(), '.todo.json');
28
+
29
+ function load(): Store {
30
+ if (!fs.existsSync(file)) return { nextId: 1, items: [] };
31
+ return JSON.parse(fs.readFileSync(file, 'utf8')) as Store;
32
+ }
33
+
34
+ // Write to a temp file, then rename over the old one: a crash mid-write never
35
+ // leaves a half-written list behind
36
+ function save(store: Store): void {
37
+ const tmp = `${file}.tmp`;
38
+ fs.writeFileSync(tmp, JSON.stringify(store, null, 2));
39
+ fs.renameSync(tmp, file);
40
+ }
41
+
42
+ function findItem(store: Store, arg: string | undefined): Item {
43
+ const id = Number(arg);
44
+ const item = store.items.find((i) => i.id === id);
45
+ if (!item) {
46
+ console.error(`todo: no item with id ${arg ?? '(none)'}`);
47
+ process.exit(1);
48
+ }
49
+ return item;
50
+ }
51
+
52
+ function usage(): void {
53
+ console.log('usage: todo add <text> | list | done <id> | rm <id>');
54
+ }
55
+
56
+ const [command = 'list', ...rest] = process.argv.slice(2);
57
+ const store = load();
58
+
59
+ if (command === 'add') {
60
+ const text = rest.join(' ').trim();
61
+ if (text === '') {
62
+ usage();
63
+ process.exit(1);
64
+ }
65
+ const item: Item = { id: store.nextId, text: text, done: false, created: new Date().toISOString() };
66
+ store.nextId += 1;
67
+ store.items.push(item);
68
+ save(store);
69
+ console.log(`added #${item.id}: ${item.text}`);
70
+ } else if (command === 'list') {
71
+ if (store.items.length === 0) {
72
+ console.log('nothing to do');
73
+ }
74
+ for (const item of store.items) {
75
+ console.log(`${item.done ? '[x]' : '[ ]'} ${String(item.id).padStart(3)} ${item.text}`);
76
+ }
77
+ const open = store.items.filter((i) => !i.done).length;
78
+ if (store.items.length > 0) console.log(`\n${open} open, ${store.items.length - open} done`);
79
+ } else if (command === 'done') {
80
+ const item = findItem(store, rest[0]);
81
+ item.done = true;
82
+ save(store);
83
+ console.log(`done #${item.id}: ${item.text}`);
84
+ } else if (command === 'rm') {
85
+ const item = findItem(store, rest[0]);
86
+ store.items = store.items.filter((i) => i.id !== item.id);
87
+ save(store);
88
+ console.log(`removed #${item.id}: ${item.text}`);
89
+ } else {
90
+ usage();
91
+ process.exit(1);
92
+ }
@@ -0,0 +1,136 @@
1
+ # yaml CLI
2
+
3
+ The [`yaml`](https://github.com/eemeli/yaml) package's own command-line tool,
4
+ compiled from its **unmodified** upstream TypeScript source: about 11,000
5
+ lines across 78 files, vendored as is in [vendor/yaml/](vendor/yaml/) (see
6
+ [VENDOR.md](vendor/yaml/VENDOR.md): no patches). [main.ts](main.ts) ports
7
+ upstream's `bin.js` to TypeScript. It hands stdin and the arguments to the
8
+ library's `cli()` and turns the error it reports into an exit code.
9
+
10
+ This is a test of TypeNative on real code nobody wrote for it: a lexer and
11
+ parser driven by sticky regexes, class hierarchies with overrides (an
12
+ `Array` subclass among them), structural object types passed between
13
+ modules, `Object.assign` and `Object.defineProperty` on class instances,
14
+ Maps keyed by nodes, and JSON output whose key order and number formatting
15
+ must match V8's.
16
+
17
+ ## Usage
18
+
19
+ ```sh
20
+ yaml < in.yaml # re-emit the YAML stream
21
+ yaml --json < in.yaml # print it as JSON
22
+ yaml valid < in.yaml # exit 1 on the first error
23
+ yaml lex < in.yaml # print the lexer's tokens
24
+ yaml cst < in.yaml # print the concrete syntax tree
25
+ yaml --help # all options (--indent, --single, --strict, …)
26
+ ```
27
+
28
+ ## Run it
29
+
30
+ With Node.js (24 or later runs the TypeScript directly):
31
+
32
+ ```sh
33
+ node main.ts --json < docker-compose.yml
34
+ ```
35
+
36
+ With TypeNative:
37
+
38
+ ```sh
39
+ npx typenative --source main.ts --output dist/yaml
40
+ ./dist/yaml --json < docker-compose.yml
41
+ ```
42
+
43
+ From a checkout of this repository:
44
+
45
+ ```sh
46
+ node bin/cli.js --source examples/yaml-cli/main.ts --output dist/yaml
47
+ ```
48
+
49
+ ## Parity with Node.js
50
+
51
+ Reproduce with `node parity.mjs [filter]`. The script fetches the
52
+ [yaml-test-suite](https://github.com/yaml/yaml-test-suite) at tag
53
+ `data-2022-01-17` into `.cache/` (it isn't committed) and compiles `main.ts`.
54
+ It then feeds each test's `in.yaml` to both runtimes in each of four modes:
55
+ default (re-emit), `--json`, `valid` and `lex`. stdout, stderr and the exit
56
+ code must match byte for byte. The only normalization is on stderr: Node's
57
+ ` at …` stack frames are dropped (a native binary has no JS frames to
58
+ print), and warnings' `(node:PID)` becomes `(node)`. Failures are written to
59
+ `.cache/failures/`.
60
+
61
+ ```
62
+ 1608/1608 cases match (100.00%)
63
+ ```
64
+
65
+ That's 402 inputs × 4 modes. They include the error cases: invalid YAML
66
+ reports the same `YAMLParseError`s (message, code, positions and the
67
+ source excerpt with its caret line) and exits with the same code. Warnings
68
+ such as `Keys with collection values will be stringified…` print the same
69
+ `process.emitWarning` lines.
70
+
71
+ ## Benchmark: Node.js vs TypeNative
72
+
73
+ Reproduce with `node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]`.
74
+ The script compiles `main.ts`, then times **complete process runs** (spawn to
75
+ exit) of `yaml --json` under both runtimes, alternating Node and native runs,
76
+ and checks that the two JSON outputs are identical:
77
+
78
+ - **(a) per-call latency:** a 1.4 KB docker-compose.yml (five services,
79
+ anchors, flow collections, a folded scalar), the kind of file a script or a
80
+ git hook converts once.
81
+ - **(b) throughput:** 10 MB of generated Kubernetes manifests (13,140
82
+ Deployments, Services and ConfigMaps in one stream, from a fixed-seed
83
+ generator, so every machine gets the same input).
84
+
85
+ **Environment:** Linux 6.18 container, 4 vCPUs of an Intel Xeon @ 2.80GHz,
86
+ Node v24.22.2, Go 1.26.0, TypeNative 0.4.2 (unreleased changes), default
87
+ (non-`--release`) build. The binary is 7.3 MB.
88
+
89
+ (a) `yaml --json < docker-compose.yml`, 30 runs each:
90
+
91
+ | Runtime | min | median | mean | max |
92
+ | ---------- | --------- | --------- | --------- | --------- |
93
+ | Node.js | 214.80 ms | 249.92 ms | 248.16 ms | 305.68 ms |
94
+ | TypeNative | 7.63 ms | 9.16 ms | 9.49 ms | 12.83 ms |
95
+
96
+ (b) `yaml --json < manifests.yaml` (10.0 MB), 5 runs each:
97
+
98
+ | Runtime | min | median | mean | max | MB/s (median) |
99
+ | ---------- | --------- | --------- | --------- | --------- | ------------- |
100
+ | Node.js | 3,916 ms | 4,226 ms | 4,137 ms | 4,301 ms | 2.37 |
101
+ | TypeNative | 14,955 ms | 15,170 ms | 15,184 ms | 15,547 ms | 0.66 |
102
+
103
+ **Per call, TypeNative is 27x faster** (9 ms vs 250 ms, median). Node's
104
+ cost here is mostly startup. It loads and type-strips the 78 TypeScript
105
+ files on every run before it reads a byte of input. That's what you feel
106
+ when a tool calls the converter once per file.
107
+
108
+ **On bulk throughput, TypeNative is 3.6x slower.** Once V8's JIT has warmed
109
+ up on a long input, it runs this code faster than the compiled Go does. The
110
+ library passes its values around as structural object types and unions
111
+ (`Scalar | YAMLMap | YAMLSeq | Pair`, CST tokens), which TypeNative holds as
112
+ `interface{}`. Property reads and calls on them go through reflection, and a
113
+ value handed to a parameter of a narrower object type is copied field by
114
+ field. Garbage collection takes about a fifth of the native run. Both costs
115
+ grow linearly with input size, while Node's head start is fixed, so the
116
+ native binary finishes first up to about 150–200 KB of YAML per process:
117
+
118
+ | Input | Node.js | TypeNative |
119
+ | ------ | ------- | ---------- |
120
+ | 16 KB | 284 ms | 38 ms |
121
+ | 64 KB | 311 ms | 107 ms |
122
+ | 128 KB | 345 ms | 253 ms |
123
+ | 1 MB | 862 ms | 1,599 ms |
124
+
125
+ (Medians of 5 full process runs each, same generator.)
126
+
127
+ Getting here fixed three performance bugs in TypeNative's runtime, all
128
+ general:
129
+
130
+ - Long strings' code-point indexes were evicted by the many short slices a
131
+ lexer takes, so every sticky-regex match rebuilt the index of the whole
132
+ input. That made lexing quadratic: about 23 s for 1 MB before (measured on
133
+ a loaded machine), about 1.5 s after.
134
+ - Struct field lookups by name are cached per type.
135
+ - `typeof f` types are func types, so the composer's callbacks are called
136
+ directly instead of through reflection.
@@ -0,0 +1,265 @@
1
+ // Benchmark: Node.js vs TypeNative for the yaml CLI, both converting YAML to
2
+ // JSON (`yaml --json`).
3
+ //
4
+ // Compiles main.ts to a native binary with the TypeNative CLI from this repo
5
+ // checkout (../../bin/cli.js), then measures:
6
+ // (a) per-call latency: complete process runs (spawn to exit) on a small
7
+ // docker-compose.yml, the kind of file a script or hook converts once;
8
+ // (b) throughput: one run on about 10 MB of generated Kubernetes-style
9
+ // manifests (Deployments, Services and ConfigMaps in one stream).
10
+ // Runs alternate Node and native, so drift on the machine hits both alike.
11
+ // Both runtimes' JSON must be identical, or the benchmark stops.
12
+ //
13
+ // Usage: node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]
14
+
15
+ import { spawnSync } from 'node:child_process';
16
+ import fs from 'node:fs';
17
+ import path from 'node:path';
18
+ import { fileURLToPath } from 'node:url';
19
+
20
+ const here = path.dirname(fileURLToPath(import.meta.url));
21
+ const repoRoot = path.resolve(here, '..', '..');
22
+ const cli = path.join(repoRoot, 'bin', 'cli.js');
23
+ const cache = path.join(here, '.cache');
24
+ const exePath = path.join(cache, process.platform === 'win32' ? 'yaml.exe' : 'yaml');
25
+ const runs = Number(process.argv[2] ?? '30') || 30;
26
+ const throughputRuns = Number(process.argv[3] ?? '5') || 5;
27
+ const megabytes = Number(process.argv[4] ?? '10') || 10;
28
+
29
+ const COMPOSE = `# A small web app: reverse proxy, API, worker, database and cache
30
+ services:
31
+ proxy:
32
+ image: nginx:1.27-alpine
33
+ ports: ["80:80", "443:443"]
34
+ volumes:
35
+ - ./nginx.conf:/etc/nginx/nginx.conf:ro
36
+ - certs:/etc/letsencrypt
37
+ depends_on: [api]
38
+ restart: unless-stopped
39
+ api:
40
+ build:
41
+ context: ./api
42
+ args:
43
+ NODE_ENV: production
44
+ environment:
45
+ DATABASE_URL: postgres://app:secret@db:5432/app
46
+ REDIS_URL: redis://cache:6379/0
47
+ LOG_LEVEL: info
48
+ WORKERS: 4
49
+ healthcheck:
50
+ test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
51
+ interval: 30s
52
+ timeout: 5s
53
+ retries: 3
54
+ depends_on:
55
+ db: { condition: service_healthy }
56
+ cache: { condition: service_started }
57
+ worker:
58
+ build: ./api
59
+ command: node dist/worker.js
60
+ environment: &worker-env
61
+ QUEUE: default
62
+ CONCURRENCY: 8
63
+ deploy:
64
+ replicas: 2
65
+ resources:
66
+ limits: { cpus: "0.50", memory: 512M }
67
+ db:
68
+ image: postgres:16
69
+ environment:
70
+ POSTGRES_USER: app
71
+ POSTGRES_PASSWORD: secret
72
+ POSTGRES_DB: app
73
+ volumes: [pgdata:/var/lib/postgresql/data]
74
+ healthcheck:
75
+ test: pg_isready -U app
76
+ interval: 10s
77
+ cache:
78
+ image: redis:7-alpine
79
+ command: >
80
+ redis-server
81
+ --maxmemory 256mb
82
+ --maxmemory-policy allkeys-lru
83
+ volumes:
84
+ pgdata: {}
85
+ certs: {}
86
+ `;
87
+
88
+ // Deterministic pseudo-random numbers (a fixed-seed LCG): the same manifests
89
+ // on every run and every machine
90
+ let seed = 42;
91
+ function rand(n) {
92
+ seed = (seed * 1103515245 + 12345) % 2147483648;
93
+ return seed % n;
94
+ }
95
+
96
+ const APPS = ['checkout', 'catalog', 'payments', 'search', 'auth', 'billing', 'notifications', 'inventory'];
97
+ const TEAMS = ['core', 'growth', 'platform', 'data'];
98
+
99
+ function deployment(name, i) {
100
+ const team = TEAMS[rand(TEAMS.length)];
101
+ const env = Array.from({ length: 6 }, (_, k) => ` - name: FEATURE_${k}_ENABLED\n value: "${rand(2) === 1}"`).join('\n');
102
+ return `apiVersion: apps/v1
103
+ kind: Deployment
104
+ metadata:
105
+ name: ${name}
106
+ namespace: ${team}
107
+ labels:
108
+ app.kubernetes.io/name: ${name}
109
+ app.kubernetes.io/part-of: shop
110
+ team: ${team}
111
+ annotations:
112
+ deployment.kubernetes.io/revision: "${1 + rand(40)}"
113
+ spec:
114
+ replicas: ${1 + rand(12)}
115
+ selector:
116
+ matchLabels:
117
+ app.kubernetes.io/name: ${name}
118
+ strategy:
119
+ type: RollingUpdate
120
+ rollingUpdate: { maxSurge: 25%, maxUnavailable: 0 }
121
+ template:
122
+ metadata:
123
+ labels:
124
+ app.kubernetes.io/name: ${name}
125
+ version: v${1 + rand(5)}.${rand(20)}.${rand(100)}
126
+ spec:
127
+ serviceAccountName: ${name}
128
+ containers:
129
+ - name: app
130
+ image: registry.example.com/shop/${name}:${(i * 7919).toString(16)}
131
+ imagePullPolicy: IfNotPresent
132
+ ports:
133
+ - { name: http, containerPort: 8080, protocol: TCP }
134
+ - { name: metrics, containerPort: 9090, protocol: TCP }
135
+ args: ["--port=8080", "--log-format=json", "--shutdown-grace=${10 + rand(50)}s"]
136
+ env:
137
+ - name: POD_NAME
138
+ valueFrom:
139
+ fieldRef: { fieldPath: metadata.name }
140
+ ${env}
141
+ resources:
142
+ requests: { cpu: ${50 + rand(450)}m, memory: ${64 + rand(448)}Mi }
143
+ limits: { cpu: "${1 + rand(3)}", memory: ${512 + rand(1536)}Mi }
144
+ readinessProbe:
145
+ httpGet: { path: /ready, port: http }
146
+ initialDelaySeconds: ${rand(15)}
147
+ periodSeconds: 10
148
+ livenessProbe:
149
+ httpGet: { path: /healthz, port: http }
150
+ failureThreshold: 3
151
+ `;
152
+ }
153
+
154
+ function service(name, team) {
155
+ return `apiVersion: v1
156
+ kind: Service
157
+ metadata:
158
+ name: ${name}
159
+ namespace: ${team}
160
+ spec:
161
+ type: ClusterIP
162
+ selector:
163
+ app.kubernetes.io/name: ${name}
164
+ ports:
165
+ - name: http
166
+ port: 80
167
+ targetPort: http
168
+ `;
169
+ }
170
+
171
+ function configMap(name, team) {
172
+ const lines = Array.from({ length: 8 }, (_, k) => ` key_${k} = ${rand(100000) / 100}`).join('\n');
173
+ return `apiVersion: v1
174
+ kind: ConfigMap
175
+ metadata:
176
+ name: ${name}-config
177
+ namespace: ${team}
178
+ data:
179
+ LOG_LEVEL: ${['debug', 'info', 'warn'][rand(3)]}
180
+ TIMEOUT_MS: "${100 * (1 + rand(50))}"
181
+ app.properties: |
182
+ ${lines}
183
+ `;
184
+ }
185
+
186
+ function manifests(bytes) {
187
+ const docs = [];
188
+ let size = 0;
189
+ for (let i = 0; size < bytes; i++) {
190
+ const name = `${APPS[i % APPS.length]}-${i}`;
191
+ const team = TEAMS[i % TEAMS.length];
192
+ for (const doc of [deployment(name, i), service(name, team), configMap(name, team)]) {
193
+ docs.push(doc);
194
+ size += doc.length + 4;
195
+ }
196
+ }
197
+ return docs.join('---\n');
198
+ }
199
+
200
+ function stats(samples) {
201
+ const sorted = [...samples].sort((a, b) => a - b);
202
+ const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
203
+ return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
204
+ }
205
+
206
+ function fmt(ms) {
207
+ return `${ms.toFixed(2)} ms`.padStart(13);
208
+ }
209
+
210
+ function runOnce(command, args, input) {
211
+ const t0 = performance.now();
212
+ const result = spawnSync(command, args, { cwd: here, input, maxBuffer: 1 << 30 });
213
+ const elapsed = performance.now() - t0;
214
+ if (result.status !== 0) {
215
+ console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
216
+ process.exit(1);
217
+ }
218
+ return { elapsed, stdout: result.stdout };
219
+ }
220
+
221
+ // Alternating Node and native runs; the two outputs must match
222
+ function timeBoth(input, count) {
223
+ const node = [];
224
+ const native = [];
225
+ for (let i = 0; i < count; i++) {
226
+ const a = runOnce(process.execPath, ['main.ts', '--json'], input);
227
+ const b = runOnce(exePath, ['--json'], input);
228
+ if (!a.stdout.equals(b.stdout)) {
229
+ console.error('Node and native JSON differ');
230
+ process.exit(1);
231
+ }
232
+ node.push(a.elapsed);
233
+ native.push(b.elapsed);
234
+ }
235
+ return [stats(node), stats(native)];
236
+ }
237
+
238
+ function table(nodeStats, nativeStats, extra = () => '') {
239
+ console.log(`| Runtime | min | median | mean | max |${extra('head')}`);
240
+ console.log(`|------------|--------------|--------------|--------------|--------------|${extra('rule')}`);
241
+ console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |${extra(nodeStats)}`);
242
+ console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |${extra(nativeStats)}`);
243
+ }
244
+
245
+ // 1. Compile the CLI to a native binary.
246
+ fs.mkdirSync(cache, { recursive: true });
247
+ console.log('Compiling main.ts to a native binary with TypeNative...');
248
+ const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', exePath], { cwd: here, stdio: 'inherit' });
249
+ if (build.status !== 0) process.exit(build.status ?? 1);
250
+
251
+ // 2. Per-call latency on a small docker-compose.yml.
252
+ const compose = Buffer.from(COMPOSE);
253
+ console.log(`\n(a) \`yaml --json < docker-compose.yml\` (${compose.length} bytes), ${runs} runs each (full process: spawn -> exit):\n`);
254
+ const [nodeCall, nativeCall] = timeBoth(compose, runs);
255
+ table(nodeCall, nativeCall);
256
+ console.log(`\nTypeNative is ${(nodeCall.median / nativeCall.median).toFixed(1)}x faster per call (median).`);
257
+
258
+ // 3. Throughput on generated Kubernetes manifests.
259
+ const big = Buffer.from(manifests(megabytes * 1024 * 1024));
260
+ const mb = big.length / (1024 * 1024);
261
+ console.log(`\n(b) \`yaml --json < manifests.yaml\` (${mb.toFixed(1)} MB, ${big.toString().split('\n---\n').length} documents), ${throughputRuns} runs each:\n`);
262
+ const [nodeBig, nativeBig] = timeBoth(big, throughputRuns);
263
+ table(nodeBig, nativeBig, (s) => (s === 'head' ? ' MB/s (median) |' : s === 'rule' ? '---------------|' : ` ${(mb / (s.median / 1000)).toFixed(2).padStart(13)} |`));
264
+ const ratio = nodeBig.median / nativeBig.median;
265
+ console.log(`\nTypeNative is ${ratio >= 1 ? `${ratio.toFixed(1)}x faster` : `${(1 / ratio).toFixed(1)}x slower`} on throughput (median).`);
@@ -0,0 +1,29 @@
1
+ // The `yaml` npm package's own command-line tool, compiled from its unmodified
2
+ // upstream TypeScript (vendor/yaml/src, see vendor/yaml/VENDOR.md). This file
3
+ // is a TypeScript port of upstream's bin.js: it hands stdin and the arguments
4
+ // to cli() and turns the error it reports into an exit code.
5
+ //
6
+ // yaml < in.yaml re-emit the YAML stream
7
+ // yaml --json < in.yaml print it as JSON
8
+ // yaml valid < in.yaml exit 1 on the first error
9
+ // yaml lex < in.yaml print the lexer's tokens
10
+ // yaml cst < in.yaml print the concrete syntax tree
11
+ //
12
+ // Runs as-is under Node.js (`node main.ts`) and compiles with TypeNative.
13
+ import { UserError, cli, help } from './vendor/yaml/src/cli.ts';
14
+
15
+ cli(
16
+ process.stdin,
17
+ (error) => {
18
+ if (error instanceof UserError) {
19
+ if (error.code === UserError.ARGS) console.error(`${help}\n`);
20
+ console.error(error.message);
21
+ process.exitCode = error.code;
22
+ } else if (error) {
23
+ // bin.js rethrows here; Node then prints the error and exits with 1
24
+ console.error(error);
25
+ process.exitCode = 1;
26
+ }
27
+ },
28
+ process.argv.slice(2)
29
+ );