typenative 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/node/assert.js +2 -2
- package/bin/node/child_process.js +66 -8
- package/bin/node/fs.js +1012 -4
- package/bin/node/histogram.js +1991 -0
- package/bin/node/process.js +14 -2
- package/bin/node/sqlite.js +1488 -5
- package/bin/node/stream.js +2 -1
- package/bin/node/system_extra.js +3 -270
- package/bin/node/test.js +39 -1
- package/bin/node/test_report.js +47 -1
- package/bin/node/test_run.js +57 -2
- package/bin/node/util.js +8 -5
- package/bin/node/webassembly.js +20 -5
- package/bin/node/webview.js +3257 -244
- package/bin/node/zlib.js +118 -14
- package/bin/node/zlib_iter.js +585 -0
- package/bin/transpiler.js +1370 -258
- package/bin/version.js +1 -1
- package/govendor/software.sslmate.com/src/go-pkcs12/.gitattributes +10 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/LICENSE +28 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/README.md +31 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/bmp-string.go +63 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/crypto.go +331 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/errors.go +23 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/go.mod +3 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/internal/rc2/rc2.go +271 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/mac.go +202 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/pbkdf.go +184 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/pkcs12.go +1000 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/safebags.go +107 -0
- package/package.json +10 -1
- package/types/tsconfig.json +17 -0
- package/types/typenative-npm.d.ts +492 -12
- package/types/typenative.d.ts +88 -2
- package/.gitattributes +0 -1
- package/.github/workflows/ci.yml +0 -68
- package/.github/workflows/npm-publish-github-packages.yml +0 -88
- package/CHANGELOG.md +0 -1746
- package/CONTRIBUTION.md +0 -133
- package/conformance.md +0 -1497
- package/examples/README.md +0 -114
- package/examples/angular-templates/README.md +0 -141
- package/examples/angular-templates/bench.mjs +0 -173
- package/examples/angular-templates/dom-type-globals.d.ts +0 -21
- package/examples/angular-templates/dom-type-stubs.ts +0 -21
- package/examples/angular-templates/main.ts +0 -135
- package/examples/angular-templates/parity.mjs +0 -157
- package/examples/angular-templates/run-node.mjs +0 -69
- package/examples/angular-templates/vendor/angular/compiler/LICENSE +0 -21
- package/examples/desktop-notes/README.md +0 -106
- package/examples/desktop-notes/bench.mjs +0 -125
- package/examples/desktop-notes/main.ts +0 -131
- package/examples/desktop-notes/package-lock.json +0 -726
- package/examples/desktop-notes/package.json +0 -9
- package/examples/desktop-notes/ui.ts +0 -176
- package/examples/dupe-finder/README.md +0 -78
- package/examples/dupe-finder/bench.mjs +0 -120
- package/examples/dupe-finder/main.ts +0 -87
- package/examples/fibonacci/README.md +0 -89
- package/examples/fibonacci/bench.mjs +0 -87
- package/examples/fibonacci/main.ts +0 -21
- package/examples/hello-world/README.md +0 -62
- package/examples/hello-world/bench.mjs +0 -66
- package/examples/hello-world/main.ts +0 -7
- package/examples/json-server/README.md +0 -132
- package/examples/json-server/bench.mjs +0 -263
- package/examples/json-server/main.ts +0 -67
- package/examples/mandelbrot-png/README.md +0 -63
- package/examples/mandelbrot-png/bench.mjs +0 -95
- package/examples/mandelbrot-png/main.ts +0 -98
- package/examples/mandelbrot-png/preview.png +0 -0
- package/examples/marked-cli/README.md +0 -174
- package/examples/marked-cli/bench.mjs +0 -178
- package/examples/marked-cli/main.ts +0 -410
- package/examples/marked-cli/parity.mjs +0 -185
- package/examples/marked-cli/vendor/marked/LICENSE +0 -44
- package/examples/marked-cli/vendor/marked/VENDOR.md +0 -26
- package/examples/openai-chat/README.md +0 -148
- package/examples/openai-chat/bench.mjs +0 -175
- package/examples/openai-chat/main.ts +0 -488
- package/examples/react-compiler/README.md +0 -185
- package/examples/react-compiler/bench.mjs +0 -181
- package/examples/react-compiler/main.ts +0 -324
- package/examples/react-compiler/parity.mjs +0 -362
- package/examples/react-compiler/run-node.mjs +0 -90
- package/examples/react-compiler/vendor/babel/LICENSE +0 -22
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +0 -15
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +0 -51
- package/examples/react-compiler/vendor/charcodes/package.json +0 -25
- package/examples/react-compiler/vendor/invariant/LICENSE +0 -21
- package/examples/react-compiler/vendor/invariant/invariant.cjs +0 -51
- package/examples/react-compiler/vendor/invariant/invariant.d.cts +0 -11
- package/examples/react-compiler/vendor/invariant/types/LICENSE +0 -21
- package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +0 -19
- package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +0 -19
- package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +0 -19
- package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +0 -19
- package/examples/react-compiler/vendor/js-tokens/LICENSE +0 -21
- package/examples/react-compiler/vendor/js-tokens/index.cjs +0 -23
- package/examples/react-compiler/vendor/js-tokens/index.d.cts +0 -13
- package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +0 -20
- package/examples/react-compiler/vendor/jsesc/jsesc.cjs +0 -337
- package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +0 -384
- package/examples/react-compiler/vendor/jsesc/types/LICENSE +0 -21
- package/examples/react-compiler/vendor/picocolors/LICENSE +0 -15
- package/examples/react-compiler/vendor/picocolors/picocolors.cjs +0 -75
- package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +0 -5
- package/examples/react-compiler/vendor/picocolors/types.d.ts +0 -51
- package/examples/react-compiler/vendor/react-compiler/LICENSE +0 -21
- package/examples/react-compiler/vendor/stubs/babel-core.ts +0 -31
- package/examples/react-compiler/vendor/stubs/debug.ts +0 -18
- package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +0 -13
- package/examples/react-compiler/vendor/stubs/pretty-format.ts +0 -18
- package/examples/react-compiler/vendor/zod/LICENSE +0 -21
- package/examples/react-compiler/vendor/zod-validation-error/LICENSE +0 -9
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +0 -5
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +0 -49
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +0 -48
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +0 -50
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +0 -6
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +0 -126
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +0 -39
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +0 -78
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +0 -2
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +0 -131
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +0 -54
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +0 -40
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +0 -76
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +0 -76
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +0 -27
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +0 -27
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +0 -13
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +0 -61
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +0 -38
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +0 -20
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +0 -5
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +0 -8
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +0 -11
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +0 -20
- package/examples/react-compiler/vendor.mjs +0 -688
- package/examples/todo-cli/README.md +0 -69
- package/examples/todo-cli/bench.mjs +0 -89
- package/examples/todo-cli/main.ts +0 -92
- package/examples/yaml-cli/README.md +0 -152
- package/examples/yaml-cli/bench.mjs +0 -265
- package/examples/yaml-cli/main.ts +0 -251
- package/examples/yaml-cli/parity.mjs +0 -156
- package/examples/yaml-cli/vendor/yaml/LICENSE +0 -13
- package/examples/yaml-cli/vendor/yaml/VENDOR.md +0 -25
- package/performance/README.md +0 -248
- package/performance/any-dynamic.ts +0 -21
- package/performance/any-record-view.ts +0 -20
- package/performance/arr-for-of.ts +0 -29
- package/performance/arr-hof.ts +0 -21
- package/performance/arr-push-index.ts +0 -15
- package/performance/arr-push-local.ts +0 -39
- package/performance/arr-slice-concat.ts +0 -24
- package/performance/arr-sort.ts +0 -21
- package/performance/async-await.ts +0 -24
- package/performance/async-promise-all.ts +0 -38
- package/performance/bigint-arith.ts +0 -36
- package/performance/buf-encode.ts +0 -29
- package/performance/class-accessors.ts +0 -48
- package/performance/class-dispatch.ts +0 -52
- package/performance/date-format.ts +0 -18
- package/performance/events-emit.ts +0 -27
- package/performance/exc-throw-rare.ts +0 -25
- package/performance/exc-try-catch.ts +0 -26
- package/performance/fn-closures.ts +0 -29
- package/performance/fn-recursion-tree.ts +0 -31
- package/performance/fn-rest-spread.ts +0 -43
- package/performance/gen-for-of.ts +0 -46
- package/performance/gen-next.ts +0 -32
- package/performance/iter-helpers.ts +0 -38
- package/performance/json-any.ts +0 -30
- package/performance/json-roundtrip.ts +0 -27
- package/performance/map-delete.ts +0 -24
- package/performance/map-iterate.ts +0 -26
- package/performance/map-number-keys.ts +0 -37
- package/performance/map-string-keys.ts +0 -22
- package/performance/num-arith-loop.ts +0 -14
- package/performance/num-bitwise.ts +0 -21
- package/performance/num-hash-imul.ts +0 -36
- package/performance/num-mod-int.ts +0 -16
- package/performance/num-to-string.ts +0 -16
- package/performance/obj-alloc.ts +0 -32
- package/performance/obj-keys-entries.ts +0 -44
- package/performance/obj-optional-chain.ts +0 -42
- package/performance/obj-record-dict.ts +0 -24
- package/performance/obj-spread-destructure.ts +0 -40
- package/performance/regex-exec.ts +0 -29
- package/performance/regex-replace-fn.ts +0 -24
- package/performance/regex-site.ts +0 -28
- package/performance/set-ops.ts +0 -20
- package/performance/str-char-scan.ts +0 -35
- package/performance/str-concat-plus.ts +0 -18
- package/performance/str-concat.ts +0 -24
- package/performance/str-methods.ts +0 -24
- package/performance/str-short-scan.ts +0 -26
- package/performance/str-split-join.ts +0 -23
- package/performance/str-switch-compare.ts +0 -50
- package/performance/str-template.ts +0 -16
- package/performance/str-unicode.ts +0 -25
- package/performance/typed-array.ts +0 -30
- package/performance/union-switch.ts +0 -57
- package/performance/union-typeof.ts +0 -40
- package/performance/wasm-call.ts +0 -63
- package/tsparser/check.go +0 -158
- package/tsparser/go.mod +0 -14
- package/tsparser/go.sum +0 -22
- package/tsparser/main.go +0 -363
- package/tsparser/tsparser.exe +0 -4
|
@@ -1,95 +0,0 @@
|
|
|
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);
|
|
@@ -1,98 +0,0 @@
|
|
|
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
|
|
@@ -1,174 +0,0 @@
|
|
|
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.7.0, default
|
|
97
|
-
(non-`--release`) build. The binary is 9.8 MB.
|
|
98
|
-
|
|
99
|
-
(a) `marked < README.md` (3.5 KB), 30 runs each:
|
|
100
|
-
|
|
101
|
-
| Runtime | min | median | mean | max |
|
|
102
|
-
| ---------- | --------- | --------- | --------- | --------- |
|
|
103
|
-
| Node.js | 104.86 ms | 106.91 ms | 108.76 ms | 122.06 ms |
|
|
104
|
-
| TypeNative | 12.05 ms | 13.10 ms | 20.19 ms | 228.66 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 | 604.66 ms | 607.99 ms | 609.90 ms | 620.93 ms | 16.45 |
|
|
111
|
-
| TypeNative | 521.38 ms | 534.45 ms | 532.89 ms | 539.79 ms | 18.71 |
|
|
112
|
-
|
|
113
|
-
**Per call, TypeNative is 8.2x faster** (13.1 ms vs 106.9 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.1x faster** (534 ms vs 608 ms). A
|
|
119
|
-
loaded document is cut before top-level ATX headings that follow a blank line
|
|
120
|
-
and are outside a code fence; up to six worker processes each parse one
|
|
121
|
-
contiguous run of sections, and their HTML is concatenated. Parsing the
|
|
122
|
-
sections separately is byte-for-byte identical to parsing the whole document
|
|
123
|
-
here (bench.mjs checks it), because marked renders top-level blocks
|
|
124
|
-
independently. `main.ts` falls back to one process for inputs under 512 KB, for
|
|
125
|
-
documents with reference link definitions, raw HTML blocks or CRLF line
|
|
126
|
-
endings, and for `--tokens`; `parity.mjs`'s spec cases all take that path, so
|
|
127
|
-
their behavior is unchanged. The workers pass their assignment in `workerData`
|
|
128
|
-
and return gzipped HTML through one temporary file: the worker IPC carries a
|
|
129
|
-
single compact message, not several megabytes.
|
|
130
|
-
|
|
131
|
-
**A single process is still about 1.6x slower than Node on 10 MB** (1.92 s vs
|
|
132
|
-
1.22 s). The
|
|
133
|
-
parallel path is what closes the gap; per section the remaining cost is
|
|
134
|
-
TypeNative's regex VM, which interprets marked's patterns where V8's Irregexp
|
|
135
|
-
compiles them to machine code, plus the object and match-array allocation each
|
|
136
|
-
token costs.
|
|
137
|
-
|
|
138
|
-
The ReDoS inputs now favor the native binary: all of them together take 0.8 s
|
|
139
|
-
natively and 3.3 s under Node, though most of Node's time there is starting up
|
|
140
|
-
once per input. The slowest native case, `backticks_alternating_in_link`, takes
|
|
141
|
-
0.22 s against Node's 0.11 s.
|
|
142
|
-
|
|
143
|
-
Getting here fixed performance bugs in TypeNative's runtime and compiler, all
|
|
144
|
-
general:
|
|
145
|
-
|
|
146
|
-
- Every regex now runs on one engine, a backtracking VM with JS semantics, in
|
|
147
|
-
place of Go's RE2 for simple patterns and a closure-based matcher for the
|
|
148
|
-
rest. A match allocates only its result. First-byte sets, literal prefixes
|
|
149
|
-
and anchors reject most calls without running anything, which matters here
|
|
150
|
-
because 83% of marked's 1.5 million regex calls per MB miss. RE2 remains as a
|
|
151
|
-
fallback: a pattern it can run that exhausts the VM's step budget switches to
|
|
152
|
-
it, so those patterns keep their linear-time guarantee.
|
|
153
|
-
- Regex literals are compiled once, however many times they're evaluated.
|
|
154
|
-
- `a += b` on a long string held as any is amortized: the string is extended
|
|
155
|
-
in place when it ends where its buffer's written part ends. It used to copy
|
|
156
|
-
on every append, which made the worst ReDoS input take over 10 s.
|
|
157
|
-
- Strings built with `+=` in a loop are `strings.Builder`s, so rendering a
|
|
158
|
-
long document is linear.
|
|
159
|
-
- Short ASCII strings need no code-point index, and slices of a long non-ASCII
|
|
160
|
-
string share its index instead of rebuilding it.
|
|
161
|
-
- Type inference is memoized per query. marked's
|
|
162
|
-
`edit(re).replace(…).replace(…)…` chains took exponential time to compile.
|
|
163
|
-
- `[...s].length`, `[...s][i]` and `[...s].at(i)` are the code point count, the
|
|
164
|
-
i-th code point and the i-th code point (from the end), without building an
|
|
165
|
-
array of every rune; marked uses those for every emphasis delimiter.
|
|
166
|
-
- `re.exec(s)` where only its presence is tested (`!!re.exec(s)`, an `if`
|
|
167
|
-
condition) runs `re.test(s)`: no match array is built.
|
|
168
|
-
- A nullable regex result is boxed only when it matched (`TnSlicePtr`), not by
|
|
169
|
-
a closure and a heap box on every miss.
|
|
170
|
-
- Greedy runs of "everything but one ASCII byte" (`[^\\n]+`, `[^*]+`) use
|
|
171
|
-
`IndexByte` instead of testing each byte.
|
|
172
|
-
- The ASCII cache for long strings is a mutex-guarded entry, not a new atomic
|
|
173
|
-
entry per inline token.
|
|
174
|
-
- Regex replacement builders are grown once to the input's length.
|
|
@@ -1,178 +0,0 @@
|
|
|
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).`);
|