typenative 0.5.0 → 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 +271 -1
- package/CONTRIBUTION.md +2 -1
- package/README.md +15 -7
- package/bin/cli.js +1 -0
- package/bin/index.js +62 -3
- package/bin/main.js +1 -0
- package/bin/node/core.js +41 -0
- package/bin/node/events.js +15 -10
- package/bin/node/process.js +13 -0
- package/bin/node/punycode.js +2 -2
- package/bin/node/timers.js +4 -2
- package/bin/node/url.js +1 -1
- package/bin/parse-node.js +2 -2
- package/bin/regex_vm.js +2057 -0
- package/bin/transpiler.js +17225 -4830
- package/bin/typecheck-node.js +2 -1
- package/bin/version.js +1 -1
- package/conformance.md +57 -15
- package/examples/README.md +46 -23
- 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 +11 -13
- package/examples/dupe-finder/README.md +7 -5
- 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 +11 -13
- package/examples/marked-cli/README.md +38 -37
- 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 +9 -9
- package/examples/yaml-cli/README.md +38 -28
- 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 +1 -0
- package/types/typenative-npm.d.ts +3 -2
- package/types/typenative.d.ts +42 -12
- package/ROUND2-HANDOFF.md +0 -200
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// perf: creating closures over mutable state and calling them indirectly
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
function makeCounter(step: number): () => number {
|
|
5
|
+
let count = 0;
|
|
6
|
+
return () => {
|
|
7
|
+
count += step;
|
|
8
|
+
return count;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function apply(f: (x: number) => number, x: number): number {
|
|
13
|
+
return f(x);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const n = 12_000_000;
|
|
17
|
+
|
|
18
|
+
const start = performance.now();
|
|
19
|
+
let total = 0;
|
|
20
|
+
for (let i = 0; i < n; i++) {
|
|
21
|
+
const next = makeCounter(i & 3);
|
|
22
|
+
next();
|
|
23
|
+
total += next();
|
|
24
|
+
total += apply((x) => x + (i & 1), 1);
|
|
25
|
+
}
|
|
26
|
+
const elapsed = performance.now() - start;
|
|
27
|
+
|
|
28
|
+
console.log(`checksum: ${total}`);
|
|
29
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// perf: building and walking binary trees (recursion plus allocation)
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
class TreeNode {
|
|
5
|
+
left: TreeNode | null;
|
|
6
|
+
right: TreeNode | null;
|
|
7
|
+
constructor(left: TreeNode | null, right: TreeNode | null) {
|
|
8
|
+
this.left = left;
|
|
9
|
+
this.right = right;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function make(depth: number): TreeNode {
|
|
14
|
+
return depth === 0 ? new TreeNode(null, null) : new TreeNode(make(depth - 1), make(depth - 1));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function check(node: TreeNode): number {
|
|
18
|
+
if (node.left === null || node.right === null) return 1;
|
|
19
|
+
return 1 + check(node.left) + check(node.right);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const start = performance.now();
|
|
23
|
+
let total = 0;
|
|
24
|
+
for (let depth = 4; depth <= 16; depth += 2) {
|
|
25
|
+
const iterations = 1 << (16 - depth + 4);
|
|
26
|
+
for (let i = 0; i < iterations; i++) total += check(make(depth));
|
|
27
|
+
}
|
|
28
|
+
const elapsed = performance.now() - start;
|
|
29
|
+
|
|
30
|
+
console.log(`checksum: ${total}`);
|
|
31
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// perf: sync generators (a method, `*[Symbol.iterator]()`, `yield*`) consumed by `for…of`
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
class Op {
|
|
5
|
+
value: number;
|
|
6
|
+
constructor(value: number) {
|
|
7
|
+
this.value = value;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class OpList {
|
|
12
|
+
private items: Op[] = [];
|
|
13
|
+
push(op: Op): void {
|
|
14
|
+
this.items.push(op);
|
|
15
|
+
}
|
|
16
|
+
*[Symbol.iterator](): Generator<Op> {
|
|
17
|
+
for (let i = 0; i < this.items.length; i++) yield this.items[i];
|
|
18
|
+
}
|
|
19
|
+
*reversed(): Generator<Op> {
|
|
20
|
+
for (let i = this.items.length - 1; i >= 0; i--) yield this.items[i];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function* range(n: number): Generator<number> {
|
|
25
|
+
for (let i = 0; i < n; i++) yield i;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function* both(list: OpList): Generator<Op> {
|
|
29
|
+
yield* list.reversed();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const list = new OpList();
|
|
33
|
+
for (let i = 0; i < 2000; i++) list.push(new Op(i));
|
|
34
|
+
|
|
35
|
+
const start = performance.now();
|
|
36
|
+
let total = 0;
|
|
37
|
+
for (let pass = 0; pass < 500; pass++) {
|
|
38
|
+
for (const op of list) total += op.value;
|
|
39
|
+
for (const op of list.reversed()) total -= op.value >> 1;
|
|
40
|
+
for (const i of range(2000)) total += i & 7;
|
|
41
|
+
for (const op of both(list)) total += op.value & 3;
|
|
42
|
+
}
|
|
43
|
+
const elapsed = performance.now() - start;
|
|
44
|
+
|
|
45
|
+
console.log(`checksum: ${total}`);
|
|
46
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// perf: `JSON.stringify` and `JSON.parse` of an array of nested objects
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
interface Item {
|
|
5
|
+
id: number;
|
|
6
|
+
name: string;
|
|
7
|
+
price: number;
|
|
8
|
+
tags: string[];
|
|
9
|
+
dims: { w: number; h: number };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const items: Item[] = [];
|
|
13
|
+
for (let i = 0; i < 5000; i++) {
|
|
14
|
+
items.push({ id: i, name: `item "${i}"`, price: i * 1.25, tags: ['x', 'y' + (i % 9)], dims: { w: i % 50, h: 3 } });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const start = performance.now();
|
|
18
|
+
let total = 0;
|
|
19
|
+
for (let pass = 0; pass < 20; pass++) {
|
|
20
|
+
const text = JSON.stringify(items);
|
|
21
|
+
const back = JSON.parse(text) as Item[];
|
|
22
|
+
total += text.length + back.length + back[pass].dims.w;
|
|
23
|
+
}
|
|
24
|
+
const elapsed = performance.now() - start;
|
|
25
|
+
|
|
26
|
+
console.log(`checksum: ${total}`);
|
|
27
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// perf: `Map<string, number>` insert/delete churn (an LRU-style sliding window)
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const keys: string[] = [];
|
|
5
|
+
for (let i = 0; i < 4096; i++) keys.push(`key-${i}`);
|
|
6
|
+
const window = 2048;
|
|
7
|
+
const n = 1_000_000;
|
|
8
|
+
|
|
9
|
+
const start = performance.now();
|
|
10
|
+
const m = new Map<string, number>();
|
|
11
|
+
let total = 0;
|
|
12
|
+
for (let i = 0; i < n; i++) {
|
|
13
|
+
m.set(keys[i & 4095], i);
|
|
14
|
+
if (i >= window) {
|
|
15
|
+
const old = keys[(i - window) & 4095];
|
|
16
|
+
if (m.delete(old)) total++;
|
|
17
|
+
}
|
|
18
|
+
if (m.has(keys[(i * 7) & 4095])) total += 2;
|
|
19
|
+
}
|
|
20
|
+
for (const [, v] of m) total += v & 15;
|
|
21
|
+
const elapsed = performance.now() - start;
|
|
22
|
+
|
|
23
|
+
console.log(`checksum: ${total * 10 + m.size}`);
|
|
24
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// perf: `Map<string, number>` `set` / `get` / `has` word counting
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const keys: string[] = [];
|
|
5
|
+
for (let i = 0; i < 50_000; i++) keys.push('word' + ((i * 7) % 20_000));
|
|
6
|
+
|
|
7
|
+
const start = performance.now();
|
|
8
|
+
let hits = 0;
|
|
9
|
+
for (let pass = 0; pass < 30; pass++) {
|
|
10
|
+
const counts = new Map<string, number>();
|
|
11
|
+
for (const k of keys) {
|
|
12
|
+
counts.set(k, (counts.get(k) ?? 0) + 1);
|
|
13
|
+
}
|
|
14
|
+
for (let i = 0; i < 25_000; i++) {
|
|
15
|
+
if (counts.has('word' + i)) hits++;
|
|
16
|
+
}
|
|
17
|
+
hits += counts.size;
|
|
18
|
+
}
|
|
19
|
+
const elapsed = performance.now() - start;
|
|
20
|
+
|
|
21
|
+
console.log(`checksum: ${hits}`);
|
|
22
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// perf: tight float64 loop with `%`, `*` and `Math.floor`
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const n = 50_000_000;
|
|
5
|
+
|
|
6
|
+
const start = performance.now();
|
|
7
|
+
let sum = 0;
|
|
8
|
+
for (let i = 0; i < n; i++) {
|
|
9
|
+
sum += Math.floor((i * 31) / 7) % 13;
|
|
10
|
+
}
|
|
11
|
+
const elapsed = performance.now() - start;
|
|
12
|
+
|
|
13
|
+
console.log(`checksum: ${sum}`);
|
|
14
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// perf: bitwise and, or, xor and shifts (`<<`, `>>`, `>>>`) on integers, and flag tests
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const F_A = 1 << 0;
|
|
5
|
+
const F_B = 1 << 3;
|
|
6
|
+
const F_C = 1 << 7;
|
|
7
|
+
const n = 20_000_000;
|
|
8
|
+
|
|
9
|
+
const start = performance.now();
|
|
10
|
+
let acc = 0;
|
|
11
|
+
let hits = 0;
|
|
12
|
+
for (let i = 0; i < n; i++) {
|
|
13
|
+
const flags = (i ^ (i >> 3)) & 0xff;
|
|
14
|
+
if ((flags & F_A) !== 0) hits++;
|
|
15
|
+
if ((flags & (F_B | F_C)) === F_B) hits += 2;
|
|
16
|
+
acc = (acc + ((i << 2) | (flags >>> 1))) & 0xffffff;
|
|
17
|
+
}
|
|
18
|
+
const elapsed = performance.now() - start;
|
|
19
|
+
|
|
20
|
+
console.log(`checksum: ${acc * 1000 + hits}`);
|
|
21
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// perf: `%` on integer operands (hash buckets, cycling indexes)
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const n = 20_000_000;
|
|
5
|
+
|
|
6
|
+
const start = performance.now();
|
|
7
|
+
let sum = 0;
|
|
8
|
+
let k = 7;
|
|
9
|
+
for (let i = 0; i < n; i++) {
|
|
10
|
+
sum += (i * 31 + k) % 1009;
|
|
11
|
+
k = (k + i) % 97;
|
|
12
|
+
}
|
|
13
|
+
const elapsed = performance.now() - start;
|
|
14
|
+
|
|
15
|
+
console.log(`checksum: ${sum}`);
|
|
16
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// perf: number/string conversion: `String(n)`, `toFixed`, `Number(str)`, `parseInt`
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const n = 1_000_000;
|
|
5
|
+
|
|
6
|
+
const start = performance.now();
|
|
7
|
+
let sum = 0;
|
|
8
|
+
for (let i = 0; i < n; i++) {
|
|
9
|
+
const s = String(i * 3);
|
|
10
|
+
const f = (i / 8).toFixed(2);
|
|
11
|
+
sum += s.length + f.length + Number(s) + parseInt(f, 10);
|
|
12
|
+
}
|
|
13
|
+
const elapsed = performance.now() - start;
|
|
14
|
+
|
|
15
|
+
console.log(`checksum: ${sum}`);
|
|
16
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// perf: allocating many short-lived object literals and class instances
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
interface Point {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class Vec {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
constructor(x: number, y: number) {
|
|
13
|
+
this.x = x;
|
|
14
|
+
this.y = y;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const n = 15_000_000;
|
|
19
|
+
|
|
20
|
+
const start = performance.now();
|
|
21
|
+
let sum = 0;
|
|
22
|
+
const keep: Point[] = [];
|
|
23
|
+
for (let i = 0; i < n; i++) {
|
|
24
|
+
const p: Point = { x: i, y: i * 2 };
|
|
25
|
+
const v = new Vec(p.y, p.x);
|
|
26
|
+
sum += v.x - v.y;
|
|
27
|
+
if ((i & 127) === 0) keep.push(p);
|
|
28
|
+
}
|
|
29
|
+
const elapsed = performance.now() - start;
|
|
30
|
+
|
|
31
|
+
console.log(`checksum: ${sum + keep.length}`);
|
|
32
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// perf: regex `test`, `exec` with groups and global `replace` over log lines
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const lines: string[] = [];
|
|
5
|
+
for (let i = 0; i < 5000; i++) {
|
|
6
|
+
const day = String((i % 28) + 1).padStart(2, '0');
|
|
7
|
+
const minute = String(i % 60).padStart(2, '0');
|
|
8
|
+
const level = i % 3 === 0 ? 'ERROR' : 'INFO';
|
|
9
|
+
lines.push(`2026-11-${day} 12:${minute} [${level}] user=${i} took ${i % 997}ms path=/api/v1/items/${i}`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const lineRe = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}) \[(\w+)\] user=(\d+) took (\d+)ms/;
|
|
13
|
+
const errorRe = /\[ERROR\]/;
|
|
14
|
+
const idRe = /\/(\d+)/g;
|
|
15
|
+
|
|
16
|
+
const start = performance.now();
|
|
17
|
+
let total = 0;
|
|
18
|
+
for (let pass = 0; pass < 60; pass++) {
|
|
19
|
+
for (const line of lines) {
|
|
20
|
+
const m = lineRe.exec(line);
|
|
21
|
+
if (m !== null) total += Number(m[8]) + m[6].length;
|
|
22
|
+
if (errorRe.test(line)) total++;
|
|
23
|
+
total += line.replace(idRe, '/:id').length;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const elapsed = performance.now() - start;
|
|
27
|
+
|
|
28
|
+
console.log(`checksum: ${total}`);
|
|
29
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// perf: `/g` regex literals evaluated inside a hot function, and `.index` on long input
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
function tokenize(line: string): number {
|
|
5
|
+
let count = 0;
|
|
6
|
+
const word = /[A-Za-z_]\w*/g;
|
|
7
|
+
let m: RegExpExecArray | null;
|
|
8
|
+
while ((m = word.exec(line)) !== null) count += m[0].length;
|
|
9
|
+
if (/^\s*#/.test(line)) count += 100;
|
|
10
|
+
return count;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const lines: string[] = [];
|
|
14
|
+
for (let i = 0; i < 2000; i++) lines.push(` item_${i} = compute(alpha, beta_${i & 7}) # note ${i}`);
|
|
15
|
+
const doc = lines.join('\n');
|
|
16
|
+
|
|
17
|
+
const start = performance.now();
|
|
18
|
+
let total = 0;
|
|
19
|
+
for (let pass = 0; pass < 200; pass++) {
|
|
20
|
+
for (const line of lines) total += tokenize(line);
|
|
21
|
+
}
|
|
22
|
+
const marker = /compute\(alpha, beta_3\)/g;
|
|
23
|
+
let m: RegExpExecArray | null;
|
|
24
|
+
while ((m = marker.exec(doc)) !== null) total += m.index & 1023;
|
|
25
|
+
const elapsed = performance.now() - start;
|
|
26
|
+
|
|
27
|
+
console.log(`checksum: ${total}`);
|
|
28
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// perf: `Set<number>` `add` / `has` / `delete` on a 16,384-entry set
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const passes = 10;
|
|
5
|
+
const n = 100_000;
|
|
6
|
+
|
|
7
|
+
const start = performance.now();
|
|
8
|
+
let total = 0;
|
|
9
|
+
for (let pass = 0; pass < passes; pass++) {
|
|
10
|
+
const s = new Set<number>();
|
|
11
|
+
for (let i = 0; i < n; i++) s.add((i * 13 + pass) & 16383);
|
|
12
|
+
let hits = 0;
|
|
13
|
+
for (let i = 0; i < n; i++) if (s.has(i)) hits++;
|
|
14
|
+
for (let i = 0; i < n; i += 2) s.delete(i);
|
|
15
|
+
total += hits * 10 + s.size;
|
|
16
|
+
}
|
|
17
|
+
const elapsed = performance.now() - start;
|
|
18
|
+
|
|
19
|
+
console.log(`checksum: ${total}`);
|
|
20
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// perf: lexer-style scan of a large string with `charCodeAt` and `str[i]`
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const chunk = 'let value_1 = foo(bar, 42) + "text" * 3.5; // comment\n';
|
|
5
|
+
let text = '';
|
|
6
|
+
for (let i = 0; i < 40_000; i++) text += chunk;
|
|
7
|
+
|
|
8
|
+
const start = performance.now();
|
|
9
|
+
let idents = 0;
|
|
10
|
+
let digits = 0;
|
|
11
|
+
let quotes = 0;
|
|
12
|
+
for (let pass = 0; pass < 15; pass++) {
|
|
13
|
+
let i = 0;
|
|
14
|
+
while (i < text.length) {
|
|
15
|
+
const c = text.charCodeAt(i);
|
|
16
|
+
if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90) || c === 95) {
|
|
17
|
+
while (i < text.length) {
|
|
18
|
+
const d = text.charCodeAt(i);
|
|
19
|
+
if (!((d >= 97 && d <= 122) || (d >= 65 && d <= 90) || d === 95 || (d >= 48 && d <= 57))) break;
|
|
20
|
+
i++;
|
|
21
|
+
}
|
|
22
|
+
idents++;
|
|
23
|
+
} else if (c >= 48 && c <= 57) {
|
|
24
|
+
digits++;
|
|
25
|
+
i++;
|
|
26
|
+
} else {
|
|
27
|
+
if (text[i] === '"') quotes++;
|
|
28
|
+
i++;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const elapsed = performance.now() - start;
|
|
33
|
+
|
|
34
|
+
console.log(`checksum: ${idents * 1000000 + digits * 1000 + quotes}`);
|
|
35
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// perf: growing strings with `s = s + x` (not `+=`), each pass starting empty
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const passes = 1000;
|
|
5
|
+
|
|
6
|
+
const start = performance.now();
|
|
7
|
+
let total = 0;
|
|
8
|
+
for (let pass = 0; pass < passes; pass++) {
|
|
9
|
+
let lines = '';
|
|
10
|
+
for (let i = 0; i < 3000; i++) {
|
|
11
|
+
lines = lines + 'line ' + i + '\n';
|
|
12
|
+
}
|
|
13
|
+
total += lines.length;
|
|
14
|
+
}
|
|
15
|
+
const elapsed = performance.now() - start;
|
|
16
|
+
|
|
17
|
+
console.log(`checksum: ${total}`);
|
|
18
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// perf: growing ~20 KB strings with `s += x` and `s = s + …` (each pass starts empty)
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const passes = 400;
|
|
5
|
+
const words = ['alpha', 'beta', 'gamma', 'delta'];
|
|
6
|
+
|
|
7
|
+
const start = performance.now();
|
|
8
|
+
let total = 0;
|
|
9
|
+
for (let pass = 0; pass < passes; pass++) {
|
|
10
|
+
let out = '';
|
|
11
|
+
for (let i = 0; i < 3000; i++) {
|
|
12
|
+
out += words[i & 3];
|
|
13
|
+
out += ' ';
|
|
14
|
+
}
|
|
15
|
+
let lines = '';
|
|
16
|
+
for (let i = 0; i < 300; i++) {
|
|
17
|
+
lines = lines + 'line ' + i + '\n';
|
|
18
|
+
}
|
|
19
|
+
total += out.length + lines.length;
|
|
20
|
+
}
|
|
21
|
+
const elapsed = performance.now() - start;
|
|
22
|
+
|
|
23
|
+
console.log(`checksum: ${total}`);
|
|
24
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// perf: `charCodeAt(i)` / `s[i]` scans of many short (< 512 byte) lines
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const lines: string[] = [];
|
|
5
|
+
for (let i = 0; i < 2000; i++) {
|
|
6
|
+
lines.push(` key_${i}: value ${i * 3} "quoted text" # a comment that pads the line out a bit more ${i}`);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const start = performance.now();
|
|
10
|
+
let idents = 0;
|
|
11
|
+
let digits = 0;
|
|
12
|
+
let quotes = 0;
|
|
13
|
+
for (let pass = 0; pass < 100; pass++) {
|
|
14
|
+
for (const line of lines) {
|
|
15
|
+
for (let i = 0; i < line.length; i++) {
|
|
16
|
+
const c = line.charCodeAt(i);
|
|
17
|
+
if ((c >= 97 && c <= 122) || c === 95) idents++;
|
|
18
|
+
else if (c >= 48 && c <= 57) digits++;
|
|
19
|
+
else if (line[i] === '"') quotes++;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const elapsed = performance.now() - start;
|
|
24
|
+
|
|
25
|
+
console.log(`checksum: ${idents * 1000000 + digits * 1000 + quotes}`);
|
|
26
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// perf: `split`, `join`, `indexOf`, `slice` and `trim` on CSV-like lines
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const lines: string[] = [];
|
|
5
|
+
for (let i = 0; i < 1000; i++) lines.push(` id${i}, name ${i}, ${i * 7}, tag-${i % 17}, done `);
|
|
6
|
+
const csv = lines.join('\n');
|
|
7
|
+
|
|
8
|
+
const start = performance.now();
|
|
9
|
+
let sum = 0;
|
|
10
|
+
for (let pass = 0; pass < 200; pass++) {
|
|
11
|
+
const rows = csv.split('\n');
|
|
12
|
+
for (const row of rows) {
|
|
13
|
+
const cells = row.split(',');
|
|
14
|
+
sum += cells.length;
|
|
15
|
+
const tag = cells[3].trim();
|
|
16
|
+
sum += tag.slice(tag.indexOf('-') + 1).length;
|
|
17
|
+
sum += cells.map((c) => c.trim()).join('|').length;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const elapsed = performance.now() - start;
|
|
21
|
+
|
|
22
|
+
console.log(`checksum: ${sum}`);
|
|
23
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// perf: template literals mixing strings, integers, floats and booleans
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const n = 1_000_000;
|
|
5
|
+
const names = ['ada', 'grace', 'linus', 'ken'];
|
|
6
|
+
|
|
7
|
+
const start = performance.now();
|
|
8
|
+
let total = 0;
|
|
9
|
+
for (let i = 0; i < n; i++) {
|
|
10
|
+
const s = `${names[i & 3]}#${i}: ${i / 4} items, ok=${(i & 1) === 0}`;
|
|
11
|
+
total += s.length;
|
|
12
|
+
}
|
|
13
|
+
const elapsed = performance.now() - start;
|
|
14
|
+
|
|
15
|
+
console.log(`checksum: ${total}`);
|
|
16
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// perf: evaluating an expression tree of a discriminated union with `switch (e.kind)`
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
interface Num {
|
|
5
|
+
kind: 'num';
|
|
6
|
+
value: number;
|
|
7
|
+
}
|
|
8
|
+
interface Add {
|
|
9
|
+
kind: 'add';
|
|
10
|
+
left: Expr;
|
|
11
|
+
right: Expr;
|
|
12
|
+
}
|
|
13
|
+
interface Mul {
|
|
14
|
+
kind: 'mul';
|
|
15
|
+
left: Expr;
|
|
16
|
+
right: Expr;
|
|
17
|
+
}
|
|
18
|
+
interface Neg {
|
|
19
|
+
kind: 'neg';
|
|
20
|
+
operand: Expr;
|
|
21
|
+
}
|
|
22
|
+
type Expr = Num | Add | Mul | Neg;
|
|
23
|
+
|
|
24
|
+
function build(depth: number, seed: number): Expr {
|
|
25
|
+
if (depth === 0) return { kind: 'num', value: (seed % 10) + 1 };
|
|
26
|
+
switch (seed % 3) {
|
|
27
|
+
case 0:
|
|
28
|
+
return { kind: 'add', left: build(depth - 1, seed * 3 + 1), right: build(depth - 1, seed + 7) };
|
|
29
|
+
case 1:
|
|
30
|
+
return { kind: 'mul', left: build(depth - 1, seed + 2), right: build(depth - 1, seed * 7 + 3) };
|
|
31
|
+
default:
|
|
32
|
+
return { kind: 'neg', operand: build(depth - 1, seed + 5) };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function evaluate(e: Expr): number {
|
|
37
|
+
switch (e.kind) {
|
|
38
|
+
case 'num':
|
|
39
|
+
return e.value;
|
|
40
|
+
case 'add':
|
|
41
|
+
return (evaluate(e.left) + evaluate(e.right)) % 1009;
|
|
42
|
+
case 'mul':
|
|
43
|
+
return ((evaluate(e.left) * evaluate(e.right)) % 1009) + 1;
|
|
44
|
+
case 'neg':
|
|
45
|
+
return 1010 - evaluate(e.operand);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const tree = build(18, 1);
|
|
50
|
+
|
|
51
|
+
const start = performance.now();
|
|
52
|
+
let total = 0;
|
|
53
|
+
for (let i = 0; i < 20; i++) total += evaluate(tree);
|
|
54
|
+
const elapsed = performance.now() - start;
|
|
55
|
+
|
|
56
|
+
console.log(`checksum: ${total}`);
|
|
57
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
package/tsparser/check.go
CHANGED
|
@@ -86,7 +86,8 @@ func runCheck(entry string, typesDir string) {
|
|
|
86
86
|
}
|
|
87
87
|
} else {
|
|
88
88
|
options = &core.CompilerOptions{
|
|
89
|
-
Target: core.
|
|
89
|
+
Target: core.ScriptTargetES2025,
|
|
90
|
+
UseDefineForClassFields: core.TSFalse,
|
|
90
91
|
Module: core.ModuleKindESNext,
|
|
91
92
|
ModuleResolution: core.ModuleResolutionKindBundler,
|
|
92
93
|
Lib: []string{},
|
package/tsparser/main.go
CHANGED
|
@@ -359,4 +359,5 @@ func lowerFirst(s string) string {
|
|
|
359
359
|
// Field renames to match the TypeScript API's names.
|
|
360
360
|
var fieldRenames = map[string]string{
|
|
361
361
|
"PostfixToken": "questionToken", // optional property `a?: string`
|
|
362
|
+
"DefaultType": "default", // type parameter default `<S = any>`
|
|
362
363
|
}
|
|
@@ -77,6 +77,7 @@ declare module 'node:buffer' {
|
|
|
77
77
|
|
|
78
78
|
declare module 'node:fs' {
|
|
79
79
|
import { Readable, Writable } from 'node:stream';
|
|
80
|
+
import { EventEmitter } from 'node:events';
|
|
80
81
|
namespace fs {
|
|
81
82
|
type PathLike = string;
|
|
82
83
|
type EncodingOption = BufferEncoding | { encoding?: BufferEncoding | null; flag?: string } | null;
|
|
@@ -247,7 +248,7 @@ declare module 'node:fs' {
|
|
|
247
248
|
encoding?: BufferEncoding;
|
|
248
249
|
signal?: AbortSignal;
|
|
249
250
|
}
|
|
250
|
-
interface FSWatcher extends
|
|
251
|
+
interface FSWatcher extends EventEmitter {
|
|
251
252
|
close(): void;
|
|
252
253
|
ref(): this;
|
|
253
254
|
unref(): this;
|
|
@@ -258,7 +259,7 @@ declare module 'node:fs' {
|
|
|
258
259
|
once(event: 'error', listener: (error: Error) => void): this;
|
|
259
260
|
once(event: 'close', listener: () => void): this;
|
|
260
261
|
}
|
|
261
|
-
interface StatWatcher extends
|
|
262
|
+
interface StatWatcher extends EventEmitter {
|
|
262
263
|
ref(): this;
|
|
263
264
|
unref(): this;
|
|
264
265
|
}
|