typenative 0.4.1 → 0.6.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/.github/workflows/ci.yml +2 -2
- package/.github/workflows/npm-publish-github-packages.yml +2 -2
- package/CHANGELOG.md +442 -1
- package/CONTRIBUTION.md +2 -1
- package/README.md +73 -25
- package/bin/cli.js +2 -0
- package/bin/index.js +75 -5
- package/bin/main.js +2 -0
- package/bin/node/assert.js +2 -2
- package/bin/node/async_hooks.js +1 -1
- package/bin/node/buffer.js +16 -5
- package/bin/node/child_process.js +7 -7
- package/bin/node/cluster.js +3 -3
- package/bin/node/core.js +41 -0
- package/bin/node/crypto_keys.js +21 -21
- package/bin/node/dgram.js +1 -1
- package/bin/node/diagnostics_channel.js +3 -3
- package/bin/node/dns.js +2 -2
- package/bin/node/events.js +18 -13
- package/bin/node/fetch.js +2 -2
- package/bin/node/fs.js +3 -3
- package/bin/node/fs_watch.js +5 -5
- package/bin/node/http.js +3 -3
- package/bin/node/http2.js +3 -3
- package/bin/node/net.js +8 -8
- package/bin/node/path.js +17 -3
- package/bin/node/process.js +47 -11
- package/bin/node/punycode.js +2 -2
- package/bin/node/readline.js +1 -1
- package/bin/node/registry.js +4 -1
- package/bin/node/sqlite.js +6 -6
- package/bin/node/stream.js +81 -15
- package/bin/node/stream_web.js +10 -10
- package/bin/node/system_extra.js +3 -3
- package/bin/node/test.js +674 -366
- package/bin/node/test_coverage.js +187 -0
- package/bin/node/test_report.js +1392 -0
- package/bin/node/test_run.js +575 -0
- package/bin/node/timers.js +8 -6
- package/bin/node/tls.js +5 -5
- package/bin/node/url.js +1 -1
- package/bin/node/util.js +31 -7
- package/bin/node/util_extra.js +7 -7
- package/bin/node/webview.js +654 -0
- package/bin/node/worker_threads.js +1 -1
- package/bin/node/zlib.js +3 -3
- package/bin/parse-node.js +6 -2
- package/bin/regex_vm.js +2057 -0
- package/bin/transpiler.js +32872 -11265
- package/bin/typecheck-node.js +2 -1
- package/bin/version.js +1 -1
- package/conformance.md +515 -345
- package/examples/README.md +71 -11
- package/examples/angular-templates/README.md +139 -0
- package/examples/angular-templates/bench.mjs +173 -0
- package/examples/angular-templates/dom-type-globals.d.ts +21 -0
- package/examples/angular-templates/dom-type-stubs.ts +21 -0
- package/examples/angular-templates/main.ts +135 -0
- package/examples/angular-templates/parity.mjs +157 -0
- package/examples/angular-templates/run-node.mjs +69 -0
- package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
- package/examples/desktop-notes/README.md +106 -0
- package/examples/desktop-notes/bench.mjs +125 -0
- package/examples/desktop-notes/main.ts +131 -0
- package/examples/desktop-notes/package-lock.json +726 -0
- package/examples/desktop-notes/package.json +9 -0
- package/examples/desktop-notes/ui.ts +176 -0
- package/examples/dupe-finder/README.md +78 -0
- package/examples/dupe-finder/bench.mjs +120 -0
- package/examples/dupe-finder/main.ts +87 -0
- package/examples/fibonacci/README.md +7 -7
- package/examples/hello-world/README.md +6 -6
- package/examples/json-server/README.md +19 -18
- package/examples/mandelbrot-png/README.md +63 -0
- package/examples/mandelbrot-png/bench.mjs +95 -0
- package/examples/mandelbrot-png/main.ts +98 -0
- package/examples/mandelbrot-png/preview.png +0 -0
- package/examples/marked-cli/README.md +160 -0
- package/examples/marked-cli/bench.mjs +178 -0
- package/examples/marked-cli/main.ts +292 -0
- package/examples/marked-cli/parity.mjs +185 -0
- package/examples/marked-cli/vendor/marked/LICENSE +44 -0
- package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
- package/examples/openai-chat/README.md +148 -0
- package/examples/openai-chat/bench.mjs +175 -0
- package/examples/openai-chat/main.ts +488 -0
- package/examples/react-compiler/README.md +170 -0
- package/examples/react-compiler/bench.mjs +181 -0
- package/examples/react-compiler/main.ts +158 -0
- package/examples/react-compiler/parity.mjs +362 -0
- package/examples/react-compiler/run-node.mjs +88 -0
- package/examples/react-compiler/vendor/babel/LICENSE +22 -0
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
- package/examples/react-compiler/vendor/charcodes/package.json +25 -0
- package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
- package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
- package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
- package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
- package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
- package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
- package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
- package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
- package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
- package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
- package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
- package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
- package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
- package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
- package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
- package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
- package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
- package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
- package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
- package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
- package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
- package/examples/react-compiler/vendor/zod/LICENSE +21 -0
- package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
- package/examples/react-compiler/vendor.mjs +688 -0
- package/examples/todo-cli/README.md +69 -0
- package/examples/todo-cli/bench.mjs +89 -0
- package/examples/todo-cli/main.ts +92 -0
- package/examples/yaml-cli/README.md +146 -0
- package/examples/yaml-cli/bench.mjs +265 -0
- package/examples/yaml-cli/main.ts +29 -0
- package/examples/yaml-cli/parity.mjs +156 -0
- package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
- package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
- package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
- package/govendor/github.com/jchv/go-webview2/README.md +21 -0
- package/govendor/github.com/jchv/go-webview2/common.go +84 -0
- package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
- package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
- package/govendor/github.com/jchv/go-winloader/README.md +124 -0
- package/govendor/github.com/jchv/go-winloader/common.go +11 -0
- package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
- package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
- package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
- package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
- package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
- package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
- package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +8 -5
- package/performance/README.md +195 -0
- package/performance/any-dynamic.ts +21 -0
- package/performance/any-record-view.ts +20 -0
- package/performance/arr-hof.ts +21 -0
- package/performance/arr-push-index.ts +15 -0
- package/performance/arr-sort.ts +21 -0
- package/performance/async-await.ts +24 -0
- package/performance/class-dispatch.ts +52 -0
- package/performance/events-emit.ts +27 -0
- package/performance/exc-throw-rare.ts +25 -0
- package/performance/exc-try-catch.ts +26 -0
- package/performance/fn-closures.ts +29 -0
- package/performance/fn-recursion-tree.ts +31 -0
- package/performance/gen-for-of.ts +46 -0
- package/performance/json-roundtrip.ts +27 -0
- package/performance/map-delete.ts +24 -0
- package/performance/map-string-keys.ts +22 -0
- package/performance/num-arith-loop.ts +14 -0
- package/performance/num-bitwise.ts +21 -0
- package/performance/num-mod-int.ts +16 -0
- package/performance/num-to-string.ts +16 -0
- package/performance/obj-alloc.ts +32 -0
- package/performance/regex-exec.ts +29 -0
- package/performance/regex-site.ts +28 -0
- package/performance/set-ops.ts +20 -0
- package/performance/str-char-scan.ts +35 -0
- package/performance/str-concat-plus.ts +18 -0
- package/performance/str-concat.ts +24 -0
- package/performance/str-short-scan.ts +26 -0
- package/performance/str-split-join.ts +23 -0
- package/performance/str-template.ts +16 -0
- package/performance/union-switch.ts +57 -0
- package/tsparser/check.go +2 -1
- package/tsparser/main.go +5 -0
- package/types/typenative-npm.d.ts +193 -4
- package/types/typenative.d.ts +138 -41
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Render benchmark: Node.js vs TypeNative for the Mandelbrot PNG renderer.
|
|
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 both runtimes rendering the same image, and checks that the two
|
|
6
|
+
// PNGs decode to identical pixels. (The compressed bytes differ: Node uses
|
|
7
|
+
// zlib's deflate and TypeNative Go's compress/flate.)
|
|
8
|
+
//
|
|
9
|
+
// Usage: node bench.mjs [width=1920] [height=1080] [runs=10]
|
|
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 zlib from 'node:zlib';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
17
|
+
|
|
18
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
19
|
+
const repoRoot = path.resolve(here, '..', '..');
|
|
20
|
+
const cli = path.join(repoRoot, 'bin', 'cli.js');
|
|
21
|
+
const width = process.argv[2] ?? '1920';
|
|
22
|
+
const height = process.argv[3] ?? '1080';
|
|
23
|
+
const runs = Number(process.argv[4] ?? '10') || 10;
|
|
24
|
+
const exeName = process.platform === 'win32' ? 'mandelbrot.exe' : 'mandelbrot';
|
|
25
|
+
const exePath = path.join(here, 'dist', exeName);
|
|
26
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mandelbrot-bench-'));
|
|
27
|
+
|
|
28
|
+
function stats(samples) {
|
|
29
|
+
const sorted = [...samples].sort((a, b) => a - b);
|
|
30
|
+
const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
|
|
31
|
+
return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function fmt(ms) {
|
|
35
|
+
return `${ms.toFixed(2)} ms`.padStart(11);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Inflated IDAT data (the raw scanlines), after checking every chunk's CRC
|
|
39
|
+
function decodePixels(file) {
|
|
40
|
+
const png = fs.readFileSync(file);
|
|
41
|
+
const idat = [];
|
|
42
|
+
for (let offset = 8; offset < png.length; ) {
|
|
43
|
+
const length = png.readUInt32BE(offset);
|
|
44
|
+
const typeAndData = png.subarray(offset + 4, offset + 8 + length);
|
|
45
|
+
if (zlib.crc32(typeAndData) !== png.readUInt32BE(offset + 8 + length)) throw new Error(`bad CRC in ${file}`);
|
|
46
|
+
if (typeAndData.toString('latin1', 0, 4) === 'IDAT') idat.push(typeAndData.subarray(4));
|
|
47
|
+
offset += 12 + length;
|
|
48
|
+
}
|
|
49
|
+
return zlib.inflateSync(Buffer.concat(idat));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function timeRuns(command, args) {
|
|
53
|
+
const samples = [];
|
|
54
|
+
for (let i = 0; i < runs; i++) {
|
|
55
|
+
const t0 = performance.now();
|
|
56
|
+
const result = spawnSync(command, args, { cwd: here, encoding: 'utf8' });
|
|
57
|
+
const elapsed = performance.now() - t0;
|
|
58
|
+
if (result.status !== 0) {
|
|
59
|
+
console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
samples.push(elapsed);
|
|
63
|
+
}
|
|
64
|
+
return stats(samples);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 1. Compile the renderer to a native binary.
|
|
68
|
+
fs.mkdirSync(path.join(here, 'dist'), { recursive: true });
|
|
69
|
+
console.log('Compiling main.ts to a native binary with TypeNative...');
|
|
70
|
+
const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', path.join('dist', exeName)], {
|
|
71
|
+
cwd: here,
|
|
72
|
+
stdio: 'inherit',
|
|
73
|
+
});
|
|
74
|
+
if (build.status !== 0) process.exit(build.status ?? 1);
|
|
75
|
+
|
|
76
|
+
// 2. Time full renders under both runtimes.
|
|
77
|
+
const nodePng = path.join(tmpDir, 'node.png');
|
|
78
|
+
const nativePng = path.join(tmpDir, 'native.png');
|
|
79
|
+
console.log(`\nTiming ${runs} full process runs of each runtime at ${width}x${height}...\n`);
|
|
80
|
+
const nodeStats = timeRuns(process.execPath, ['main.ts', width, height, nodePng]);
|
|
81
|
+
const nativeStats = timeRuns(exePath, [width, height, nativePng]);
|
|
82
|
+
|
|
83
|
+
const nodePixels = decodePixels(nodePng);
|
|
84
|
+
const same = nodePixels.equals(decodePixels(nativePng));
|
|
85
|
+
const sizes = [fs.statSync(nodePng).size, fs.statSync(nativePng).size];
|
|
86
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
87
|
+
|
|
88
|
+
console.log(`Render + PNG encode + write, ${runs} runs (full process: spawn -> exit):\n`);
|
|
89
|
+
console.log('| Runtime | min | median | mean | max |');
|
|
90
|
+
console.log('|------------|-------------|-------------|-------------|-------------|');
|
|
91
|
+
console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |`);
|
|
92
|
+
console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |`);
|
|
93
|
+
console.log(`\nPNG sizes: Node ${sizes[0]} B, TypeNative ${sizes[1]} B; decoded pixels identical: ${same}`);
|
|
94
|
+
console.log(`TypeNative is ${(nodeStats.median / nativeStats.median).toFixed(1)}x faster (median).`);
|
|
95
|
+
if (!same) process.exit(1);
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Renders the Mandelbrot set to a PNG file. The PNG encoder is written here
|
|
2
|
+
// by hand on top of node:zlib (deflate + CRC-32), so the program needs no
|
|
3
|
+
// image library under Node.js or TypeNative.
|
|
4
|
+
//
|
|
5
|
+
// node main.ts [width=1920] [height=1080] [out=mandelbrot.png]
|
|
6
|
+
// ./mandelbrot [width] [height] [out]
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import zlib from 'node:zlib';
|
|
9
|
+
|
|
10
|
+
const width = Number(process.argv[2] ?? '1920');
|
|
11
|
+
const height = Number(process.argv[3] ?? '1080');
|
|
12
|
+
const out = process.argv[4] ?? 'mandelbrot.png';
|
|
13
|
+
const maxIter = 500;
|
|
14
|
+
|
|
15
|
+
// Smooth escape-time colouring: a cosine palette indexed by the
|
|
16
|
+
// continuous iteration count, black inside the set
|
|
17
|
+
function colorAt(cx: number, cy: number): [number, number, number] {
|
|
18
|
+
let x = 0;
|
|
19
|
+
let y = 0;
|
|
20
|
+
let i = 0;
|
|
21
|
+
while (x * x + y * y <= 256 && i < maxIter) {
|
|
22
|
+
const xt = x * x - y * y + cx;
|
|
23
|
+
y = 2 * x * y + cy;
|
|
24
|
+
x = xt;
|
|
25
|
+
i++;
|
|
26
|
+
}
|
|
27
|
+
if (i === maxIter) return [0, 0, 0];
|
|
28
|
+
const smooth = i + 1 - Math.log2(Math.log2(x * x + y * y) / 2);
|
|
29
|
+
const t = 0.55 + Math.sqrt(smooth) * 0.11;
|
|
30
|
+
const r = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.0))));
|
|
31
|
+
const g = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.1))));
|
|
32
|
+
const b = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.2))));
|
|
33
|
+
return [r, g, b];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Raw PNG scanlines: each row is a filter byte (0 = none) then RGB triples
|
|
37
|
+
function render(): Uint8Array {
|
|
38
|
+
const stride = width * 3 + 1;
|
|
39
|
+
const pixels = new Uint8Array(stride * height);
|
|
40
|
+
const scale = 3 / height;
|
|
41
|
+
for (let py = 0; py < height; py++) {
|
|
42
|
+
const cy = (py - height / 2) * scale;
|
|
43
|
+
let offset = py * stride + 1;
|
|
44
|
+
for (let px = 0; px < width; px++) {
|
|
45
|
+
const cx = (px - width * 0.65) * scale;
|
|
46
|
+
const [r, g, b] = colorAt(cx, cy);
|
|
47
|
+
pixels[offset] = r;
|
|
48
|
+
pixels[offset + 1] = g;
|
|
49
|
+
pixels[offset + 2] = b;
|
|
50
|
+
offset += 3;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return pixels;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// One PNG chunk: length, type, data, CRC-32 of type + data
|
|
57
|
+
function chunk(type: string, data: Uint8Array): Uint8Array {
|
|
58
|
+
const typed = new Uint8Array(4 + data.length);
|
|
59
|
+
for (let i = 0; i < 4; i++) typed[i] = type.charCodeAt(i);
|
|
60
|
+
typed.set(data, 4);
|
|
61
|
+
const result = new Uint8Array(12 + data.length);
|
|
62
|
+
const view = new DataView(result.buffer);
|
|
63
|
+
view.setUint32(0, data.length);
|
|
64
|
+
result.set(typed, 4);
|
|
65
|
+
view.setUint32(8 + data.length, zlib.crc32(typed));
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function encodePng(pixels: Uint8Array): Uint8Array {
|
|
70
|
+
const header = new Uint8Array(13);
|
|
71
|
+
const view = new DataView(header.buffer);
|
|
72
|
+
view.setUint32(0, width);
|
|
73
|
+
view.setUint32(4, height);
|
|
74
|
+
header[8] = 8; // bit depth
|
|
75
|
+
header[9] = 2; // colour type: truecolour RGB
|
|
76
|
+
const parts = [
|
|
77
|
+
new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
78
|
+
chunk('IHDR', header),
|
|
79
|
+
chunk('IDAT', new Uint8Array(zlib.deflateSync(pixels))),
|
|
80
|
+
chunk('IEND', new Uint8Array(0))
|
|
81
|
+
];
|
|
82
|
+
const total = parts.reduce((sum, p) => sum + p.length, 0);
|
|
83
|
+
const png = new Uint8Array(total);
|
|
84
|
+
let offset = 0;
|
|
85
|
+
for (const part of parts) {
|
|
86
|
+
png.set(part, offset);
|
|
87
|
+
offset += part.length;
|
|
88
|
+
}
|
|
89
|
+
return png;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const t0 = performance.now();
|
|
93
|
+
const pixels = render();
|
|
94
|
+
const t1 = performance.now();
|
|
95
|
+
const png = encodePng(pixels);
|
|
96
|
+
fs.writeFileSync(out, png);
|
|
97
|
+
const t2 = performance.now();
|
|
98
|
+
console.log(`Rendered ${width}x${height} in ${Math.round(t1 - t0)} ms, encoded ${png.length} bytes in ${Math.round(t2 - t1)} ms -> ${out}`);
|
|
Binary file
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# marked CLI
|
|
2
|
+
|
|
3
|
+
The [`marked`](https://github.com/markedjs/marked) package's own command-line
|
|
4
|
+
tool, compiled from its **unmodified** upstream TypeScript source: about 4,000
|
|
5
|
+
lines across 13 files, vendored as is in [vendor/marked/](vendor/marked/) (see
|
|
6
|
+
[VENDOR.md](vendor/marked/VENDOR.md): no patches). [main.ts](main.ts) ports
|
|
7
|
+
upstream's `bin/main.js` to TypeScript. It parses the same options, reads the
|
|
8
|
+
markdown from a string, a file or stdin, and prints the HTML or the lexer's
|
|
9
|
+
token list.
|
|
10
|
+
|
|
11
|
+
This is the second test of TypeNative on real code nobody wrote for it. marked
|
|
12
|
+
is a markdown parser built on regexes: 66 definitions in `rules.ts`, with GFM
|
|
13
|
+
and pedantic variants, many composed at run time by a fluent
|
|
14
|
+
`edit(…).replace(…)` builder (44 `edit` calls). Many use lookarounds or backreferences, which
|
|
15
|
+
Go's RE2 engine doesn't support, so they run on TypeNative's backtracking
|
|
16
|
+
engine. Tokens are structural object types, arrays of tokens are
|
|
17
|
+
filled in later through a queue that holds the same arrays, and the `--tokens`
|
|
18
|
+
JSON must match V8's key order and leave out `undefined` fields, such as the
|
|
19
|
+
title of a link definition that has none.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
marked < in.md # render markdown to HTML
|
|
25
|
+
marked -s '*hi*' # render a string
|
|
26
|
+
marked -i in.md -o out.html # read a file, write a file
|
|
27
|
+
marked --tokens < in.md # print the token list as JSON
|
|
28
|
+
marked --no-gfm --pedantic # any boolean option of marked.defaults
|
|
29
|
+
marked --help # all options
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Differences from `bin/main.js` (listed in main.ts's header): `--help` prints an
|
|
33
|
+
option summary instead of opening the man page, `--version` prints the vendored
|
|
34
|
+
version, and a config file (`-c`, `~/.marked.json`) must be JSON. A JavaScript
|
|
35
|
+
config is a module Node would import and run, which a native binary can't. The
|
|
36
|
+
HTML is also flushed before the process exits, under Node too: `bin/main.js`
|
|
37
|
+
exits right after writing, which cuts output to a pipe off at 64 KB.
|
|
38
|
+
|
|
39
|
+
## Run it
|
|
40
|
+
|
|
41
|
+
With Node.js (24 or later runs the TypeScript directly):
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
node main.ts < README.md
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
With TypeNative:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
npx typenative --source main.ts --output dist/marked
|
|
51
|
+
./dist/marked < README.md
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
From a checkout of this repository:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
node bin/cli.js --source examples/marked-cli/main.ts --output dist/marked
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Parity with Node.js
|
|
61
|
+
|
|
62
|
+
Reproduce with `node parity.mjs [filter]`. The script fetches marked's own spec
|
|
63
|
+
suites (`test/specs` at tag `v18.1.2`) into `.cache/` (it isn't committed) and
|
|
64
|
+
compiles `main.ts`. It then feeds each case's markdown on stdin to both
|
|
65
|
+
runtimes in three modes: default (GFM), `--no-gfm` and `--tokens`. A case's
|
|
66
|
+
front matter (`gfm`, `pedantic`, `breaks`) becomes CLI flags, as marked's test
|
|
67
|
+
runner turns it into options. stdout, stderr and the exit code must match byte
|
|
68
|
+
for byte. The only normalization is on stderr: Node's ` at …` stack frames
|
|
69
|
+
are dropped (a native binary has no JS frames to print). Each run has a 10 s
|
|
70
|
+
timeout. Failures are written to `.cache/failures/`.
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
2859/2859 cases match (100.00%)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
That's 953 inputs × 3 modes: CommonMark 0.31.2 (652 examples), GFM 0.29 (28),
|
|
77
|
+
marked's own `new` (223) and `original` (22) tests, and its 28 ReDoS
|
|
78
|
+
regression inputs. The ReDoS inputs are pathological on purpose, such as 10,000
|
|
79
|
+
underscores in a row. Every one of them finishes within the timeout natively
|
|
80
|
+
too (see the benchmark notes below for how long they take).
|
|
81
|
+
|
|
82
|
+
## Benchmark: Node.js vs TypeNative
|
|
83
|
+
|
|
84
|
+
Reproduce with `node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]`.
|
|
85
|
+
The script compiles `main.ts`, then times **complete process runs** (spawn to
|
|
86
|
+
exit) of `marked` under both runtimes, alternating Node and native runs, and
|
|
87
|
+
checks that the two HTML outputs are identical:
|
|
88
|
+
|
|
89
|
+
- **(a) per-call latency:** marked's own README.md (3.5 KB), the kind of file
|
|
90
|
+
a docs build or a git hook renders once per file.
|
|
91
|
+
- **(b) throughput:** 10 MB of generated markdown (headings, paragraphs with
|
|
92
|
+
emphasis, links and code spans, lists, tables, code fences and block quotes,
|
|
93
|
+
from a fixed-seed generator, so every machine gets the same input).
|
|
94
|
+
|
|
95
|
+
**Environment:** Windows 11 Pro, AMD Ryzen 5 7600 (6 cores, 12 threads),
|
|
96
|
+
Node v26.13.0, Go 1.27.4, TypeNative 0.5.0 (unreleased changes), default
|
|
97
|
+
(non-`--release`) build. The binary is 6.5 MB.
|
|
98
|
+
|
|
99
|
+
(a) `marked < README.md` (3.5 KB), 30 runs each:
|
|
100
|
+
|
|
101
|
+
| Runtime | min | median | mean | max |
|
|
102
|
+
| ---------- | --------- | --------- | --------- | --------- |
|
|
103
|
+
| Node.js | 112.27 ms | 118.78 ms | 119.48 ms | 128.69 ms |
|
|
104
|
+
| TypeNative | 13.10 ms | 14.12 ms | 20.74 ms | 210.83 ms |
|
|
105
|
+
|
|
106
|
+
(b) `marked < generated.md` (10.0 MB, 114,893 lines), 5 runs each:
|
|
107
|
+
|
|
108
|
+
| Runtime | min | median | mean | max | MB/s (median) |
|
|
109
|
+
| ---------- | -------- | -------- | -------- | -------- | ------------- |
|
|
110
|
+
| Node.js | 1,187 ms | 1,197 ms | 1,197 ms | 1,206 ms | 8.35 |
|
|
111
|
+
| TypeNative | 1,880 ms | 1,890 ms | 1,909 ms | 1,968 ms | 5.29 |
|
|
112
|
+
|
|
113
|
+
**Per call, TypeNative is 8.4x faster** (14 ms vs 119 ms, median). Node's
|
|
114
|
+
cost here is mostly startup: it loads and type-strips the 13 TypeScript files
|
|
115
|
+
and compiles marked's regexes on every run. That's what you feel when a docs
|
|
116
|
+
build or a hook renders one file per process.
|
|
117
|
+
|
|
118
|
+
**On bulk throughput, TypeNative is 1.6x slower.** On a long input V8's JIT
|
|
119
|
+
and its regex engine (Irregexp compiles hot patterns to machine code) still run
|
|
120
|
+
marked faster than the compiled Go does. In a profile of the native run on
|
|
121
|
+
10 MB, about 30% of the main thread is TypeNative's regex VM, which interprets
|
|
122
|
+
marked's patterns where Irregexp runs them as machine code, and allocation is
|
|
123
|
+
about 15%: each token is an object and each match a few strings. Garbage
|
|
124
|
+
collection runs on other cores and hardly affects the wall clock. Both costs
|
|
125
|
+
grow linearly with the input, while Node's head start is fixed, so the native
|
|
126
|
+
binary finishes first up to about 1–1.5 MB of markdown per process:
|
|
127
|
+
|
|
128
|
+
| Input | Node.js | TypeNative |
|
|
129
|
+
| ------ | ------- | ---------- |
|
|
130
|
+
| 512 KB | 162 ms | 100 ms |
|
|
131
|
+
| 1 MB | 203 ms | 186 ms |
|
|
132
|
+
| 2 MB | 294 ms | 359 ms |
|
|
133
|
+
|
|
134
|
+
(Medians of 5 full process runs each, same generator.)
|
|
135
|
+
|
|
136
|
+
The ReDoS inputs now favor the native binary: all of them together take 4.7 s
|
|
137
|
+
natively and 13 s under Node, though most of Node's time there is starting up
|
|
138
|
+
once per input. The slowest native case, `quadratic_inline_masking`, takes
|
|
139
|
+
0.7 s against Node's 0.9 s.
|
|
140
|
+
|
|
141
|
+
Getting here fixed performance bugs in TypeNative's runtime and compiler, all
|
|
142
|
+
general:
|
|
143
|
+
|
|
144
|
+
- Every regex now runs on one engine, a backtracking VM with JS semantics, in
|
|
145
|
+
place of Go's RE2 for simple patterns and a closure-based matcher for the
|
|
146
|
+
rest. A match allocates only its result. First-byte sets, literal prefixes
|
|
147
|
+
and anchors reject most calls without running anything, which matters here
|
|
148
|
+
because 83% of marked's 1.5 million regex calls per MB miss. RE2 remains as a
|
|
149
|
+
fallback: a pattern it can run that exhausts the VM's step budget switches to
|
|
150
|
+
it, so those patterns keep their linear-time guarantee.
|
|
151
|
+
- Regex literals are compiled once, however many times they're evaluated.
|
|
152
|
+
- `a += b` on a long string held as any is amortized: the string is extended
|
|
153
|
+
in place when it ends where its buffer's written part ends. It used to copy
|
|
154
|
+
on every append, which made the worst ReDoS input take over 10 s.
|
|
155
|
+
- Strings built with `+=` in a loop are `strings.Builder`s, so rendering a
|
|
156
|
+
long document is linear.
|
|
157
|
+
- Short ASCII strings need no code-point index, and slices of a long non-ASCII
|
|
158
|
+
string share its index instead of rebuilding it.
|
|
159
|
+
- Type inference is memoized per query. marked's
|
|
160
|
+
`edit(re).replace(…).replace(…)…` chains took exponential time to compile.
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Benchmark: Node.js vs TypeNative for the marked CLI, both rendering
|
|
2
|
+
// markdown to HTML (`marked < in.md`).
|
|
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 marked's
|
|
7
|
+
// own README.md, the kind of file a docs build or a hook renders once;
|
|
8
|
+
// (b) throughput: one run on about 10 MB of generated markdown (headings,
|
|
9
|
+
// paragraphs with emphasis, links and code spans, lists, tables, code
|
|
10
|
+
// fences and block quotes).
|
|
11
|
+
// Runs alternate Node and native, so drift on the machine hits both alike.
|
|
12
|
+
// Both runtimes' HTML must be identical, or the benchmark stops.
|
|
13
|
+
//
|
|
14
|
+
// Usage: node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]
|
|
15
|
+
|
|
16
|
+
import { spawnSync } from 'node:child_process';
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
|
|
21
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
const repoRoot = path.resolve(here, '..', '..');
|
|
23
|
+
const cli = path.join(repoRoot, 'bin', 'cli.js');
|
|
24
|
+
const cache = path.join(here, '.cache');
|
|
25
|
+
const upstream = path.join(cache, 'marked-src');
|
|
26
|
+
const exePath = path.join(cache, process.platform === 'win32' ? 'marked.exe' : 'marked');
|
|
27
|
+
const runs = Number(process.argv[2] ?? '30') || 30;
|
|
28
|
+
const throughputRuns = Number(process.argv[3] ?? '5') || 5;
|
|
29
|
+
const megabytes = Number(process.argv[4] ?? '10') || 10;
|
|
30
|
+
|
|
31
|
+
// marked's README.md at the vendored tag (parity.mjs fetches the same checkout)
|
|
32
|
+
function readme() {
|
|
33
|
+
const file = path.join(upstream, 'README.md');
|
|
34
|
+
if (!fs.existsSync(file)) {
|
|
35
|
+
fs.mkdirSync(cache, { recursive: true });
|
|
36
|
+
console.log('Fetching marked v18.1.2 ...');
|
|
37
|
+
const clone = spawnSync('git', ['clone', '--quiet', '--depth', '1', '--branch', 'v18.1.2', '--filter=blob:none', '--sparse', 'https://github.com/markedjs/marked.git', upstream], { stdio: 'inherit' });
|
|
38
|
+
if (clone.status !== 0) process.exit(1);
|
|
39
|
+
spawnSync('git', ['-C', upstream, 'sparse-checkout', 'set', 'test/specs'], { stdio: 'inherit' });
|
|
40
|
+
}
|
|
41
|
+
return fs.readFileSync(file);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Deterministic pseudo-random numbers (a fixed-seed LCG): the same document
|
|
45
|
+
// on every run and every machine
|
|
46
|
+
let seed = 42;
|
|
47
|
+
function rand(n) {
|
|
48
|
+
seed = (seed * 1103515245 + 12345) % 2147483648;
|
|
49
|
+
return Math.floor(seed / 65536) % n;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const WORDS = ['markdown', 'parser', 'native', 'binary', 'token', 'lexer', 'render', 'inline', 'block', 'heading',
|
|
53
|
+
'paragraph', 'emphasis', 'table', 'fence', 'quote', 'list', 'link', 'image', 'escape', 'entity', 'spec', 'html'];
|
|
54
|
+
|
|
55
|
+
function word() {
|
|
56
|
+
return WORDS[rand(WORDS.length)];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function sentence() {
|
|
60
|
+
const parts = [];
|
|
61
|
+
const n = 6 + rand(14);
|
|
62
|
+
for (let i = 0; i < n; i++) {
|
|
63
|
+
const w = word();
|
|
64
|
+
switch (rand(12)) {
|
|
65
|
+
case 0: parts.push(`*${w}*`); break;
|
|
66
|
+
case 1: parts.push(`**${w}**`); break;
|
|
67
|
+
case 2: parts.push(`\`${w}()\``); break;
|
|
68
|
+
case 3: parts.push(`[${w}](https://example.com/${w}/${rand(1000)} "${w}")`); break;
|
|
69
|
+
case 4: parts.push(`_${w}_`); break;
|
|
70
|
+
default: parts.push(w);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return parts.join(' ') + '.';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function section(i) {
|
|
77
|
+
const lines = [`${'#'.repeat(1 + rand(3))} ${word()} ${word()} ${i}`, ''];
|
|
78
|
+
for (let p = 0; p < 2 + rand(3); p++) lines.push(`${sentence()} ${sentence()}\n${sentence()}`, '');
|
|
79
|
+
switch (rand(5)) {
|
|
80
|
+
case 0:
|
|
81
|
+
for (let k = 0; k < 3 + rand(5); k++) lines.push(`- ${sentence()}`);
|
|
82
|
+
lines.push('');
|
|
83
|
+
break;
|
|
84
|
+
case 1:
|
|
85
|
+
for (let k = 1; k <= 3 + rand(4); k++) lines.push(`${k}. ${sentence()}`);
|
|
86
|
+
lines.push('');
|
|
87
|
+
break;
|
|
88
|
+
case 2:
|
|
89
|
+
lines.push(`| ${word()} | ${word()} | ${word()} |`, '| --- | :---: | ---: |');
|
|
90
|
+
for (let k = 0; k < 3 + rand(6); k++) lines.push(`| ${word()} | ${rand(1000)} | *${word()}* |`);
|
|
91
|
+
lines.push('');
|
|
92
|
+
break;
|
|
93
|
+
case 3:
|
|
94
|
+
lines.push('```js', `function ${word()}(${word()}) {`, ` return ${rand(100)} < 3 && "${word()}";`, '}', '```', '');
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
lines.push(`> ${sentence()}`, `> ${sentence()}`, '');
|
|
98
|
+
}
|
|
99
|
+
return lines.join('\n');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function document(bytes) {
|
|
103
|
+
const out = [];
|
|
104
|
+
let size = 0;
|
|
105
|
+
for (let i = 0; size < bytes; i++) {
|
|
106
|
+
const s = section(i);
|
|
107
|
+
out.push(s);
|
|
108
|
+
size += s.length + 1;
|
|
109
|
+
}
|
|
110
|
+
return out.join('\n');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function stats(samples) {
|
|
114
|
+
const sorted = [...samples].sort((a, b) => a - b);
|
|
115
|
+
const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
|
|
116
|
+
return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function fmt(ms) {
|
|
120
|
+
return `${ms.toFixed(2)} ms`.padStart(13);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function runOnce(command, args, input) {
|
|
124
|
+
const t0 = performance.now();
|
|
125
|
+
const result = spawnSync(command, args, { cwd: here, input, maxBuffer: 1 << 30 });
|
|
126
|
+
const elapsed = performance.now() - t0;
|
|
127
|
+
if (result.status !== 0) {
|
|
128
|
+
console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
|
|
129
|
+
process.exit(1);
|
|
130
|
+
}
|
|
131
|
+
return { elapsed, stdout: result.stdout };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Alternating Node and native runs; the two outputs must match
|
|
135
|
+
function timeBoth(input, count) {
|
|
136
|
+
const node = [];
|
|
137
|
+
const native = [];
|
|
138
|
+
for (let i = 0; i < count; i++) {
|
|
139
|
+
const a = runOnce(process.execPath, ['main.ts'], input);
|
|
140
|
+
const b = runOnce(exePath, [], input);
|
|
141
|
+
if (!a.stdout.equals(b.stdout)) {
|
|
142
|
+
console.error('Node and native HTML differ');
|
|
143
|
+
process.exit(1);
|
|
144
|
+
}
|
|
145
|
+
node.push(a.elapsed);
|
|
146
|
+
native.push(b.elapsed);
|
|
147
|
+
}
|
|
148
|
+
return [stats(node), stats(native)];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function table(nodeStats, nativeStats, extra = () => '') {
|
|
152
|
+
console.log(`| Runtime | min | median | mean | max |${extra('head')}`);
|
|
153
|
+
console.log(`|------------|--------------|--------------|--------------|--------------|${extra('rule')}`);
|
|
154
|
+
console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |${extra(nodeStats)}`);
|
|
155
|
+
console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |${extra(nativeStats)}`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 1. Compile the CLI to a native binary.
|
|
159
|
+
fs.mkdirSync(cache, { recursive: true });
|
|
160
|
+
const small = readme();
|
|
161
|
+
console.log('Compiling main.ts to a native binary with TypeNative...');
|
|
162
|
+
const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', exePath], { cwd: here, stdio: 'inherit' });
|
|
163
|
+
if (build.status !== 0) process.exit(build.status ?? 1);
|
|
164
|
+
|
|
165
|
+
// 2. Per-call latency on marked's README.md.
|
|
166
|
+
console.log(`\n(a) \`marked < README.md\` (${small.length} bytes), ${runs} runs each (full process: spawn -> exit):\n`);
|
|
167
|
+
const [nodeCall, nativeCall] = timeBoth(small, runs);
|
|
168
|
+
table(nodeCall, nativeCall);
|
|
169
|
+
console.log(`\nTypeNative is ${(nodeCall.median / nativeCall.median).toFixed(1)}x faster per call (median).`);
|
|
170
|
+
|
|
171
|
+
// 3. Throughput on generated markdown.
|
|
172
|
+
const big = Buffer.from(document(megabytes * 1024 * 1024));
|
|
173
|
+
const mb = big.length / (1024 * 1024);
|
|
174
|
+
console.log(`\n(b) \`marked < generated.md\` (${mb.toFixed(1)} MB, ${big.toString().split('\n').length} lines), ${throughputRuns} runs each:\n`);
|
|
175
|
+
const [nodeBig, nativeBig] = timeBoth(big, throughputRuns);
|
|
176
|
+
table(nodeBig, nativeBig, (s) => (s === 'head' ? ' MB/s (median) |' : s === 'rule' ? '---------------|' : ` ${(mb / (s.median / 1000)).toFixed(2).padStart(13)} |`));
|
|
177
|
+
const ratio = nodeBig.median / nativeBig.median;
|
|
178
|
+
console.log(`\nTypeNative is ${ratio >= 1 ? `${ratio.toFixed(1)}x faster` : `${(1 / ratio).toFixed(1)}x slower`} on throughput (median).`);
|