typenative 0.5.0 → 0.6.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/.github/workflows/ci.yml +2 -2
- package/.github/workflows/npm-publish-github-packages.yml +2 -2
- package/CHANGELOG.md +339 -0
- package/CONTRIBUTION.md +2 -1
- package/README.md +16 -7
- package/bin/cli.js +1 -0
- package/bin/index.js +62 -3
- package/bin/main.js +1 -0
- package/bin/node/core.js +46 -0
- package/bin/node/events.js +154 -15
- package/bin/node/http.js +1265 -127
- package/bin/node/process.js +13 -0
- package/bin/node/punycode.js +2 -2
- package/bin/node/stream.js +56 -7
- package/bin/node/stream_web.js +2 -2
- package/bin/node/timers.js +4 -2
- package/bin/node/url.js +28 -3
- package/bin/parse-node.js +2 -2
- package/bin/regex_vm.js +2105 -0
- package/bin/transpiler.js +17970 -4419
- package/bin/typecheck-node.js +2 -1
- package/bin/version.js +1 -1
- package/conformance.md +57 -15
- package/examples/README.md +50 -28
- package/examples/angular-templates/README.md +141 -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 +22 -18
- package/examples/mandelbrot-png/README.md +11 -13
- package/examples/marked-cli/README.md +60 -44
- package/examples/marked-cli/main.ts +122 -4
- 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 +185 -0
- package/examples/react-compiler/bench.mjs +181 -0
- package/examples/react-compiler/main.ts +324 -0
- package/examples/react-compiler/parity.mjs +362 -0
- package/examples/react-compiler/run-node.mjs +90 -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 +50 -34
- package/examples/yaml-cli/main.ts +236 -14
- package/package.json +8 -5
- package/performance/README.md +196 -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,21 @@
|
|
|
1
|
+
// perf: property reads and writes on `any` objects (nested objects, arrays)
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const records: any[] = [];
|
|
5
|
+
for (let i = 0; i < 10_000; i++) {
|
|
6
|
+
records.push({ id: i, name: 'item' + i, price: i % 100, tags: ['a', 'b'], meta: { score: i % 7 } });
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const start = performance.now();
|
|
10
|
+
let total = 0;
|
|
11
|
+
for (let pass = 0; pass < 2500; pass++) {
|
|
12
|
+
for (const r of records) {
|
|
13
|
+
total += r.price + r.meta.score + r.tags.length;
|
|
14
|
+
r.price = r.price < 99 ? r.price + 1 : 0;
|
|
15
|
+
if (r.name.length > 6) total++;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const elapsed = performance.now() - start;
|
|
19
|
+
|
|
20
|
+
console.log(`checksum: ${total}`);
|
|
21
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// perf: viewing an `any` object as `Record<string, any>` and reading keys through it
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const records: any[] = [];
|
|
5
|
+
for (let i = 0; i < 10_000; i++) {
|
|
6
|
+
records.push({ id: i, name: 'item' + i, price: i % 100, tags: ['a', 'b'] });
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const start = performance.now();
|
|
10
|
+
let total = 0;
|
|
11
|
+
for (let pass = 0; pass < 200; pass++) {
|
|
12
|
+
for (const r of records) {
|
|
13
|
+
const bag: Record<string, any> = r;
|
|
14
|
+
total += bag['price'] + bag['name'].length;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const elapsed = performance.now() - start;
|
|
18
|
+
|
|
19
|
+
console.log(`checksum: ${total}`);
|
|
20
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// perf: `map` / `filter` / `reduce` / `some` chains with closure callbacks
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const data: number[] = [];
|
|
5
|
+
for (let i = 0; i < 100_000; i++) data.push((i * 7919) % 10007);
|
|
6
|
+
|
|
7
|
+
const start = performance.now();
|
|
8
|
+
let total = 0;
|
|
9
|
+
for (let pass = 0; pass < 50; pass++) {
|
|
10
|
+
const limit = 5000 + pass;
|
|
11
|
+
const result = data
|
|
12
|
+
.map((x) => x * 2)
|
|
13
|
+
.filter((x) => x > limit)
|
|
14
|
+
.reduce((acc, x) => acc + (x & 127), 0);
|
|
15
|
+
total += result;
|
|
16
|
+
if (data.some((x) => x === limit)) total++;
|
|
17
|
+
}
|
|
18
|
+
const elapsed = performance.now() - start;
|
|
19
|
+
|
|
20
|
+
console.log(`checksum: ${total}`);
|
|
21
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// perf: `push`, indexed reads/writes and `length` on number arrays
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const n = 5_000_000;
|
|
5
|
+
|
|
6
|
+
const start = performance.now();
|
|
7
|
+
const a: number[] = [];
|
|
8
|
+
for (let i = 0; i < n; i++) a.push(i & 1023);
|
|
9
|
+
for (let i = 1; i < a.length; i++) a[i] = (a[i] + a[i - 1]) & 4095;
|
|
10
|
+
let sum = 0;
|
|
11
|
+
for (let i = 0; i < a.length; i += 3) sum += a[i];
|
|
12
|
+
const elapsed = performance.now() - start;
|
|
13
|
+
|
|
14
|
+
console.log(`checksum: ${sum}`);
|
|
15
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// perf: `sort` of numbers with a comparator and of strings in default order
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
const nums: number[] = [];
|
|
5
|
+
const strs: string[] = [];
|
|
6
|
+
let seed = 12345;
|
|
7
|
+
for (let i = 0; i < 300_000; i++) {
|
|
8
|
+
seed = (seed * 1103515245 + 12345) % 2147483648;
|
|
9
|
+
nums.push(seed % 1000000);
|
|
10
|
+
strs.push('k' + (seed % 100000));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const start = performance.now();
|
|
14
|
+
const a = nums.slice();
|
|
15
|
+
a.sort((x, y) => x - y);
|
|
16
|
+
const b = strs.slice();
|
|
17
|
+
b.sort();
|
|
18
|
+
const elapsed = performance.now() - start;
|
|
19
|
+
|
|
20
|
+
console.log(`checksum: ${a[0] + a[a.length >> 1] + a[a.length - 1]} ${b[0]} ${b[b.length >> 1]} ${b[b.length - 1]}`);
|
|
21
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// perf: `await` chains of async functions that resolve immediately
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
async function leaf(n: number): Promise<number> {
|
|
5
|
+
return n & 15;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async function middle(n: number): Promise<number> {
|
|
9
|
+
const a = await leaf(n);
|
|
10
|
+
const b = await leaf(n + 1);
|
|
11
|
+
return a + b;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function main(): Promise<void> {
|
|
15
|
+
const n = 1_000_000;
|
|
16
|
+
const start = performance.now();
|
|
17
|
+
let total = 0;
|
|
18
|
+
for (let i = 0; i < n; i++) total += await middle(i);
|
|
19
|
+
const elapsed = performance.now() - start;
|
|
20
|
+
console.log(`checksum: ${total}`);
|
|
21
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
main();
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// perf: virtual method calls through an abstract base class and an interface
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
interface Shape {
|
|
5
|
+
area(): number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
abstract class Base implements Shape {
|
|
9
|
+
abstract area(): number;
|
|
10
|
+
scaled(k: number): number {
|
|
11
|
+
return this.area() * k;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class Square extends Base {
|
|
16
|
+
side: number;
|
|
17
|
+
constructor(side: number) {
|
|
18
|
+
super();
|
|
19
|
+
this.side = side;
|
|
20
|
+
}
|
|
21
|
+
area(): number {
|
|
22
|
+
return this.side * this.side;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class Rect extends Base {
|
|
27
|
+
w: number;
|
|
28
|
+
h: number;
|
|
29
|
+
constructor(w: number, h: number) {
|
|
30
|
+
super();
|
|
31
|
+
this.w = w;
|
|
32
|
+
this.h = h;
|
|
33
|
+
}
|
|
34
|
+
area(): number {
|
|
35
|
+
return this.w * this.h;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const shapes: Base[] = [];
|
|
40
|
+
for (let i = 0; i < 1000; i++) shapes.push(i % 2 === 0 ? new Square(i % 10) : new Rect(i % 7, 3));
|
|
41
|
+
const asIface: Shape[] = shapes;
|
|
42
|
+
|
|
43
|
+
const start = performance.now();
|
|
44
|
+
let total = 0;
|
|
45
|
+
for (let pass = 0; pass < 25000; pass++) {
|
|
46
|
+
for (let i = 0; i < shapes.length; i++) total += shapes[i].scaled(2);
|
|
47
|
+
for (const s of asIface) total += s.area();
|
|
48
|
+
}
|
|
49
|
+
const elapsed = performance.now() - start;
|
|
50
|
+
|
|
51
|
+
console.log(`checksum: ${total}`);
|
|
52
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// perf: `EventEmitter` `emit` to listeners taking 0, 1 and 2 arguments
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { performance } from 'node:perf_hooks';
|
|
4
|
+
|
|
5
|
+
const bus = new EventEmitter();
|
|
6
|
+
let total = 0;
|
|
7
|
+
bus.on('tick', () => {
|
|
8
|
+
total++;
|
|
9
|
+
});
|
|
10
|
+
bus.on('data', (n: number) => {
|
|
11
|
+
total += n & 7;
|
|
12
|
+
});
|
|
13
|
+
bus.on('pair', (a: number, b: string) => {
|
|
14
|
+
total += a + b.length;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const n = 3_000_000;
|
|
18
|
+
const start = performance.now();
|
|
19
|
+
for (let i = 0; i < n; i++) {
|
|
20
|
+
bus.emit('tick');
|
|
21
|
+
bus.emit('data', i);
|
|
22
|
+
bus.emit('pair', i & 3, 'ab');
|
|
23
|
+
}
|
|
24
|
+
const elapsed = performance.now() - start;
|
|
25
|
+
|
|
26
|
+
console.log(`checksum: ${total}`);
|
|
27
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// perf: `try`/`catch`/`finally` in a hot loop where nothing throws
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
function step(i: number): number {
|
|
5
|
+
if (i < 0) throw new Error('negative');
|
|
6
|
+
return i & 255;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const n = 50_000_000;
|
|
10
|
+
const start = performance.now();
|
|
11
|
+
let total = 0;
|
|
12
|
+
let cleanups = 0;
|
|
13
|
+
for (let i = 0; i < n; i++) {
|
|
14
|
+
try {
|
|
15
|
+
total += step(i);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
total -= 1;
|
|
18
|
+
} finally {
|
|
19
|
+
cleanups++;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const elapsed = performance.now() - start;
|
|
23
|
+
|
|
24
|
+
console.log(`checksum: ${total + cleanups}`);
|
|
25
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// perf: `try`/`catch`/`finally` in a hot loop where one call in 1,024 throws
|
|
2
|
+
import { performance } from 'node:perf_hooks';
|
|
3
|
+
|
|
4
|
+
function risky(i: number): number {
|
|
5
|
+
if ((i & 1023) === 0) throw new Error('bad ' + i);
|
|
6
|
+
return i & 7;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const n = 10_000_000;
|
|
10
|
+
|
|
11
|
+
const start = performance.now();
|
|
12
|
+
let total = 0;
|
|
13
|
+
let caught = 0;
|
|
14
|
+
for (let i = 0; i < n; i++) {
|
|
15
|
+
try {
|
|
16
|
+
total += risky(i);
|
|
17
|
+
} catch (e) {
|
|
18
|
+
caught++;
|
|
19
|
+
} finally {
|
|
20
|
+
total++;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const elapsed = performance.now() - start;
|
|
24
|
+
|
|
25
|
+
console.log(`checksum: ${total + caught}`);
|
|
26
|
+
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -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`);
|