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
package/performance/README.md
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
# Performance probes
|
|
2
|
-
|
|
3
|
-
Micro-benchmarks for TypeNative's hot paths. [conformance/](../conformance/)
|
|
4
|
-
proves that a construct compiles correctly, and this folder measures how fast the
|
|
5
|
-
compiled code runs. Each probe is one small standalone TypeScript program that
|
|
6
|
-
exercises one construct (string `+=`, `Map` lookups, `switch` over a
|
|
7
|
-
discriminated union, `any` property access, …) and runs unchanged under Node.js
|
|
8
|
-
and as a TypeNative binary. A transpiler or runtime change can then be measured
|
|
9
|
-
on the exact path it touches, instead of only through whole programs like
|
|
10
|
-
[examples/marked-cli](../examples/marked-cli/) where no single hot spot stands
|
|
11
|
-
out.
|
|
12
|
-
|
|
13
|
-
## Running
|
|
14
|
-
|
|
15
|
-
From a checkout, after `npm run build`:
|
|
16
|
-
|
|
17
|
-
```sh
|
|
18
|
-
npm run perf # every probe, 7 interleaved runs each
|
|
19
|
-
node scripts/performance.mjs str # only probes whose name contains "str"
|
|
20
|
-
node scripts/performance.mjs map --runs 15 # more runs for a tighter median
|
|
21
|
-
node scripts/performance.mjs --write # also update the results table below
|
|
22
|
-
node scripts/performance.mjs --native # compile with the self-hosted binary
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
The runner ([scripts/performance.mjs](../scripts/performance.mjs)) builds each
|
|
26
|
-
probe once with the TypeNative CLI. It then runs the probe under Node.js and as
|
|
27
|
-
the native binary, alternating which runtime goes first in each round. It
|
|
28
|
-
prints a markdown table of the in-program timings. A probe stops taking more
|
|
29
|
-
rounds once its native runs have used 15 s, so a pathological probe can't stall
|
|
30
|
-
the suite. Those rows show how many runs they got.
|
|
31
|
-
|
|
32
|
-
The runner fails (exit 1) only when a probe doesn't build, crashes, or prints a
|
|
33
|
-
different `checksum` under the two runtimes. A slow probe never fails it,
|
|
34
|
-
because slowness is the thing being measured.
|
|
35
|
-
|
|
36
|
-
`--write` replaces the rows of the probes that ran inside the results region
|
|
37
|
-
below, keeps the other rows, and refreshes the environment line. Run the full
|
|
38
|
-
suite on an idle machine before committing new numbers.
|
|
39
|
-
|
|
40
|
-
## Writing a probe
|
|
41
|
-
|
|
42
|
-
A probe is `performance/<area>-<name>.ts`. Areas: `num`, `bigint`, `str`,
|
|
43
|
-
`arr`, `typed`, `buf`, `map`, `set`, `obj`, `class`, `union`, `any`, `fn`,
|
|
44
|
-
`gen`, `iter`, `regex`, `json`, `date`, `exc`, `events`, `async`, `wasm`.
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
// perf: one line saying what this measures (shown in the results table)
|
|
48
|
-
import { performance } from 'node:perf_hooks';
|
|
49
|
-
|
|
50
|
-
const input = /* build the input here, outside the timed region */;
|
|
51
|
-
|
|
52
|
-
const start = performance.now();
|
|
53
|
-
let total = 0;
|
|
54
|
-
/* the hot loop, folding its results into total */
|
|
55
|
-
const elapsed = performance.now() - start;
|
|
56
|
-
|
|
57
|
-
console.log(`checksum: ${total}`);
|
|
58
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
- **Line 1** must be `// perf: <description>`. The table's "Measures" column
|
|
62
|
-
is read from it.
|
|
63
|
-
- **Time only the hot loop.** Process startup is excluded on purpose, because
|
|
64
|
-
[examples/hello-world](../examples/hello-world/) already measures that.
|
|
65
|
-
- **Derive the checksum from the work**, so neither compiler can drop the loop.
|
|
66
|
-
The runner compares the checksums, which also catches wrong results.
|
|
67
|
-
- **Size it for Node.js at roughly 100–500 ms.** Shorter runs are noisy. If
|
|
68
|
-
TypeNative takes many seconds on it, that is a finding: shrink the probe only
|
|
69
|
-
until a run takes a few seconds, and note it below.
|
|
70
|
-
- **Measure one thing.** Keep `%` out of hot loops: it compiles to Go's slow
|
|
71
|
-
`math.Mod` (see [findings](#findings)) and would swamp whatever the probe is
|
|
72
|
-
about. Use `i & 1023` and friends instead.
|
|
73
|
-
- Keep it standalone (no local imports), like the conformance probes.
|
|
74
|
-
|
|
75
|
-
## Methodology and caveats
|
|
76
|
-
|
|
77
|
-
- Numbers are the program's own `performance.now()` timings, never spawn-to-exit
|
|
78
|
-
times. Speedups compare medians, and the minimums are listed too because they
|
|
79
|
-
are the most stable figure on a noisy machine.
|
|
80
|
-
- Background load skews results a lot. A self-hosted compile or a .NET build
|
|
81
|
-
running in the background cut both runtimes about 3x on the reference
|
|
82
|
-
machine. Interleaving the runtimes spreads drift across both, but it can't
|
|
83
|
-
remove load, so check that the machine is idle before trusting a run.
|
|
84
|
-
- Micro-benchmarks flatter JITs: V8 optimizes these tight loops with type
|
|
85
|
-
feedback that a large program rarely gets. Use the ratios here to rank hot
|
|
86
|
-
paths against each other and to track them over time. They are not a
|
|
87
|
-
prediction for whole programs; see [examples/](../examples/) for those.
|
|
88
|
-
- To find out *why* a probe (or any program) is slow, build it with `--profile`:
|
|
89
|
-
the binary writes a CPU profile to `$TYPENATIVE_CPUPROFILE` (default
|
|
90
|
-
`cpu.pprof`) when it exits. Read it with `go tool pprof -top -cum <exe> <profile>`.
|
|
91
|
-
For example, `node bin/cli.js --source performance/num-bitwise.ts --output bw.exe --profile`.
|
|
92
|
-
`--emit-go <dir>` shows the Go the profile's lines refer to.
|
|
93
|
-
|
|
94
|
-
## Results
|
|
95
|
-
|
|
96
|
-
<!-- perf-results:start -->
|
|
97
|
-
|
|
98
|
-
Last run 2026-11-17: Windows_NT 10.0.26300, AMD Ryzen 5 7600 6-Core Processor, Node v26.13.0, go1.27.4 windows/amd64, TypeNative 0.7.0, 15 interleaved runs per probe.
|
|
99
|
-
|
|
100
|
-
| Probe | Measures | Node ms (min / median) | TypeNative ms (min / median) | TypeNative vs Node (median) |
|
|
101
|
-
| ----- | -------- | ---------------------: | ---------------------------: | :-------------------------- |
|
|
102
|
-
| [any-dynamic](any-dynamic.ts) | property reads and writes on `any` objects (nested objects, arrays) | 50.3 / 51.0 | 5924 / 5956 (3 runs) | 116.8x slower |
|
|
103
|
-
| [any-record-view](any-record-view.ts) | viewing an `any` object as `Record<string, any>` and reading keys through it | 5.3 / 6.1 | 50.9 / 51.9 | 8.5x slower |
|
|
104
|
-
| [arr-for-of](arr-for-of.ts) | `for…of`, `forEach`, `find` / `findIndex` and `entries()` over arrays of objects | 53.8 / 54.7 | 270 / 275 | 5.0x slower |
|
|
105
|
-
| [arr-hof](arr-hof.ts) | `map` / `filter` / `reduce` / `some` chains with closure callbacks | 86.9 / 87.7 | 27.1 / 28.2 | **3.1x faster** |
|
|
106
|
-
| [arr-push-index](arr-push-index.ts) | `push`, indexed reads/writes and `length` on number arrays | 50.8 / 52.8 | 47.8 / 49.4 | **1.1x faster** |
|
|
107
|
-
| [arr-push-local](arr-push-local.ts) | building many short arrays with `push` inside functions (locals and fields of new objects) | 138 / 139 | 310 / 317 | 2.3x slower |
|
|
108
|
-
| [arr-slice-concat](arr-slice-concat.ts) | `slice`, `splice`, `concat`, `[...a, ...b]`, `includes` and `indexOf` on arrays | 109 / 110 | 117 / 119 | 1.1x slower |
|
|
109
|
-
| [arr-sort](arr-sort.ts) | `sort` of numbers with a comparator and of strings in default order | 105 / 116 | 308 / 314 | 2.7x slower |
|
|
110
|
-
| [async-await](async-await.ts) | `await` chains of async functions that resolve immediately | 97.6 / 99.4 | 200 / 202 | 2.0x slower |
|
|
111
|
-
| [async-promise-all](async-promise-all.ts) | `Promise.all` fan-out, `new Promise` + `.then` chains and `queueMicrotask` (the general async path) | 61.7 / 63.3 | 689 / 696 | 11.0x slower |
|
|
112
|
-
| [bigint-arith](bigint-arith.ts) | `bigint` `+`, `*`, `%` and shifts (Fibonacci mod a prime, modular exponentiation) | 70.4 / 72.1 | 686 / 698 | 9.7x slower |
|
|
113
|
-
| [buf-encode](buf-encode.ts) | `Buffer` integer reads and writes, `hex` / `base64` / `utf8` conversion, `Buffer.concat`, `Uint8Array` `toHex` / `fromBase64` and `TextEncoder` / `TextDecoder` | 78.6 / 81.0 | 94.2 / 97.5 | 1.2x slower |
|
|
114
|
-
| [class-accessors](class-accessors.ts) | getters and setters, static fields and `super.m()` calls on class instances | 9.6 / 9.7 | 2836 / 2859 (6 runs) | 294.9x slower |
|
|
115
|
-
| [class-dispatch](class-dispatch.ts) | virtual method calls through an abstract base class and an interface | 67.0 / 69.3 | 121 / 122 | 1.8x slower |
|
|
116
|
-
| [date-format](date-format.ts) | `new Date(ms)`, UTC getters, `toISOString` and `Date.parse` (timestamps in log lines) | 82.5 / 83.5 | 30.5 / 31.6 | **2.6x faster** |
|
|
117
|
-
| [events-emit](events-emit.ts) | `EventEmitter` `emit` to listeners taking 0, 1 and 2 arguments | 69.2 / 70.7 | 621 / 627 | 8.9x slower |
|
|
118
|
-
| [exc-throw-rare](exc-throw-rare.ts) | `try`/`catch`/`finally` in a hot loop where nothing throws | 191 / 193 | 243 / 245 | 1.3x slower |
|
|
119
|
-
| [exc-try-catch](exc-try-catch.ts) | `try`/`catch`/`finally` in a hot loop where one call in 1,024 throws | 23.3 / 23.9 | 62.1 / 63.3 | 2.6x slower |
|
|
120
|
-
| [fn-closures](fn-closures.ts) | creating closures over mutable state and calling them indirectly | 140 / 141 | 34.9 / 36.5 | **3.9x faster** |
|
|
121
|
-
| [fn-recursion-tree](fn-recursion-tree.ts) | building and walking binary trees (recursion plus allocation) | 99.2 / 100 | 148 / 150 | 1.5x slower |
|
|
122
|
-
| [fn-rest-spread](fn-rest-spread.ts) | rest parameters, spread arguments `f(...args)`, default parameters and `.call` / `.apply` / `.bind` | 72.1 / 72.7 | 444 / 449 | 6.2x slower |
|
|
123
|
-
| [gen-for-of](gen-for-of.ts) | sync generators (a method, `*[Symbol.iterator]()`, `yield*`) consumed by `for…of` | 51.4 / 51.8 | 11.8 / 12.2 | **4.2x faster** |
|
|
124
|
-
| [gen-next](gen-next.ts) | generators driven by hand (`.next()`), spread `[...gen()]` and stored in a variable before `for…of` | 44.0 / 44.8 | 449 / 454 | 10.1x slower |
|
|
125
|
-
| [iter-helpers](iter-helpers.ts) | ES2025 iterator helpers (`filter` / `map` / `take` / `drop` / `reduce` / `toArray`) on generators and `Iterator.from(array)` | 65.9 / 67.0 | 1042 / 1061 | 15.8x slower |
|
|
126
|
-
| [json-any](json-any.ts) | `JSON.parse` into `any` and walking the result, `JSON.stringify` of objects built as `any` | 66.0 / 67.4 | 276 / 280 | 4.2x slower |
|
|
127
|
-
| [json-roundtrip](json-roundtrip.ts) | `JSON.stringify` and `JSON.parse` of an array of nested objects | 37.5 / 38.4 | 76.7 / 78.4 | 2.0x slower |
|
|
128
|
-
| [map-delete](map-delete.ts) | `Map<string, number>` insert/delete churn (an LRU-style sliding window) | 29.9 / 35.3 | 167 / 170 | 4.8x slower |
|
|
129
|
-
| [map-iterate](map-iterate.ts) | iterating a `Map` (`for…of` entries, `forEach`, `[...values()]`), also after deletes | 85.7 / 87.1 | 229 / 231 | 2.7x slower |
|
|
130
|
-
| [map-number-keys](map-number-keys.ts) | `Map<number, T>` lookups by integer id and memoization | 107 / 108 | 165 / 169 | 1.6x slower |
|
|
131
|
-
| [map-string-keys](map-string-keys.ts) | `Map<string, number>` `set` / `get` / `has` word counting | 90.2 / 91.8 | 118 / 119 | 1.3x slower |
|
|
132
|
-
| [num-arith-loop](num-arith-loop.ts) | tight float64 loop with `%`, `*` and `Math.floor` | 73.1 / 73.7 | 155 / 157 | 2.1x slower |
|
|
133
|
-
| [num-bitwise](num-bitwise.ts) | bitwise and, or, xor and shifts (`<<`, `>>`, `>>>`) on integers, and flag tests | 20.8 / 21.8 | 108 / 109 | 5.0x slower |
|
|
134
|
-
| [num-hash-imul](num-hash-imul.ts) | FNV-1a and murmur-style string hashing with `Math.imul`, `>>> 0` and `charCodeAt` | 69.2 / 71.1 | 787 / 791 | 11.1x slower |
|
|
135
|
-
| [num-mod-int](num-mod-int.ts) | `%` on integer operands (hash buckets, cycling indexes) | 60.0 / 60.5 | 175 / 180 | 3.0x slower |
|
|
136
|
-
| [num-to-string](num-to-string.ts) | number/string conversion: `String(n)`, `toFixed`, `Number(str)`, `parseInt` | 97.8 / 101 | 390 / 404 | 4.0x slower |
|
|
137
|
-
| [obj-alloc](obj-alloc.ts) | allocating many short-lived object literals and class instances | 61.9 / 63.5 | 182 / 183 | 2.9x slower |
|
|
138
|
-
| [obj-keys-entries](obj-keys-entries.ts) | `Object.keys` / `values` / `entries` and `for…in` over typed objects and class instances | 74.2 / 75.2 | 1041 / 1053 | 14.0x slower |
|
|
139
|
-
| [obj-optional-chain](obj-optional-chain.ts) | `?.` and `??` walking nullable links and optional fields | 47.0 / 48.7 | 169 / 171 | 3.5x slower |
|
|
140
|
-
| [obj-record-dict](obj-record-dict.ts) | a `Record<string, number>` object literal used as a dictionary (`rec[k] ?? 0`, `k in rec`, `Object.keys`) | 79.3 / 84.6 | 108 / 111 | 1.3x slower |
|
|
141
|
-
| [obj-spread-destructure](obj-spread-destructure.ts) | object spread `{ ...p, x }`, destructuring with defaults, and array-destructuring swaps | 50.6 / 51.5 | 1547 / 1555 (10 runs) | 30.2x slower |
|
|
142
|
-
| [regex-exec](regex-exec.ts) | regex `test`, `exec` with groups and global `replace` over log lines | 63.9 / 65.8 | 100 / 102 | 1.6x slower |
|
|
143
|
-
| [regex-replace-fn](regex-replace-fn.ts) | `replace` with a callback, `matchAll` with groups and `split` on a regex | 69.9 / 70.4 | 77.2 / 79.1 | 1.1x slower |
|
|
144
|
-
| [regex-site](regex-site.ts) | `/g` regex literals evaluated inside a hot function, and `.index` on long input | 67.7 / 69.5 | 155 / 158 | 2.3x slower |
|
|
145
|
-
| [set-ops](set-ops.ts) | `Set<number>` `add` / `has` / `delete` on a 16,384-entry set | 36.5 / 37.3 | 86.8 / 89.4 | 2.4x slower |
|
|
146
|
-
| [str-char-scan](str-char-scan.ts) | lexer-style scan of a large string with `charCodeAt` and `str[i]` | 76.8 / 77.5 | 320 / 324 | 4.2x slower |
|
|
147
|
-
| [str-concat](str-concat.ts) | growing ~20 KB strings with `s += x` and `s = s + …` (each pass starts empty) | 9.8 / 10.2 | 22.5 / 24.0 | 2.4x slower |
|
|
148
|
-
| [str-concat-plus](str-concat-plus.ts) | growing strings with `s = s + x` (not `+=`), each pass starting empty | 28.1 / 29.4 | 92.1 / 95.4 | 3.2x slower |
|
|
149
|
-
| [str-methods](str-methods.ts) | `startsWith` / `endsWith` / `includes`, case conversion, `padStart`, `repeat` and `replaceAll` with a string | 66.3 / 67.0 | 110 / 113 | 1.7x slower |
|
|
150
|
-
| [str-short-scan](str-short-scan.ts) | `charCodeAt(i)` / `s[i]` scans of many short (< 512 byte) lines | 29.9 / 30.7 | 199 / 201 | 6.5x slower |
|
|
151
|
-
| [str-split-join](str-split-join.ts) | `split`, `join`, `indexOf`, `slice` and `trim` on CSV-like lines | 55.3 / 56.6 | 36.6 / 38.5 | **1.5x faster** |
|
|
152
|
-
| [str-switch-compare](str-switch-compare.ts) | `switch` over string values and `===` / `<` string comparisons (keyword lookup) | 88.6 / 90.7 | 92.4 / 93.8 | 1.0x slower |
|
|
153
|
-
| [str-template](str-template.ts) | template literals mixing strings, integers, floats and booleans | 51.0 / 51.5 | 81.0 / 82.2 | 1.6x slower |
|
|
154
|
-
| [str-unicode](str-unicode.ts) | `length`, `charCodeAt`, `codePointAt` and `slice` on non-ASCII text (UTF-16 indexes) | 68.0 / 71.0 | 1280 / 1302 (12 runs) | 18.3x slower |
|
|
155
|
-
| [typed-array](typed-array.ts) | `Float64Array` / `Int32Array` / `Uint8Array` indexed loops, `subarray`, `set` and `fill` | 58.7 / 60.2 | 463 / 466 | 7.7x slower |
|
|
156
|
-
| [union-switch](union-switch.ts) | evaluating an expression tree of a discriminated union with `switch (e.kind)` | 90.7 / 95.1 | 309 / 339 | 3.6x slower |
|
|
157
|
-
| [union-typeof](union-typeof.ts) | `typeof`, `instanceof` and `Array.isArray` narrowing of a five-way union (string, number, boolean, class, array) | 49.0 / 49.4 | 415 / 421 | 8.5x slower |
|
|
158
|
-
| [wasm-call](wasm-call.ts) | calls from TypeScript into WebAssembly exports (i32 / f64 arithmetic, memory loads) and reads of the exported memory | 38.9 / 39.2 | 1579 / 1589 (10 runs) | 40.5x slower |
|
|
159
|
-
|
|
160
|
-
<!-- perf-results:end -->
|
|
161
|
-
|
|
162
|
-
## Findings
|
|
163
|
-
|
|
164
|
-
Causes behind the worst rows. Each one is checked against the generated Go
|
|
165
|
-
(`--emit-go`), a `--profile` CPU profile, or the runtime source in
|
|
166
|
-
[src/transpiler.ts](../src/transpiler.ts). Update this section when a fix
|
|
167
|
-
lands or a new cause is confirmed. Before/after figures are native medians on
|
|
168
|
-
the reference machine.
|
|
169
|
-
|
|
170
|
-
### Fixed
|
|
171
|
-
|
|
172
|
-
| Probe | Cause | Fix | Before → after |
|
|
173
|
-
| ----- | ----- | --- | -------------- |
|
|
174
|
-
| `num-bitwise` (and every probe that uses `&`) | Every bitwise operand went through `TnInt32`, which called `math.Mod(f, 2^32)`: about 90% of the probe's time. | `TnInt32` converts in-range values directly and falls back to `math.Mod` only past ±2^63. Literal operands fold at compile time, and nested bitwise expressions stay `int32` between operators. | 2160 → 129 ms |
|
|
175
|
-
| `num-arith-loop`, `num-mod-int` | `a % b` compiled to `math.Mod`, whose cost grows with the exponent gap between the operands (about 150 ns for `2e8 % 13`). | `TnMod`: an `int64` remainder when both operands are integers that fit, keeping a `-0` result; `math.Mod` otherwise. | 4527 → 162 ms; 2827 → 188 ms |
|
|
176
|
-
| `gen-for-of` | A generator ran as a goroutine sending on a channel, in lockstep with the loop: two goroutine hand-offs and two `sync.Map` lookups per value. | A sync generator also compiles to a push form, `name__seq(args)`: a Go range function whose `yield` runs the loop body. `for…of` over a direct generator call (or a class's `*[Symbol.iterator]()`) uses it. A break unwinds the generator through its `finally` blocks, and a throw from the loop body passes its `catch` clauses, as in JS. `.next()`, spread and stored generators keep the channel form. | 1843 → 12 ms |
|
|
177
|
-
| `set-ops`, `map-delete` | `TnMap.Delete` scanned and spliced the insertion-order key slice, so each delete was O(size). Every `Get`/`Has`/`Set` also called `reflect.TypeFor[K]().Kind()`. | Deletes mark the key's slot dead in O(1) (an index map built on the first delete of a map past 16 keys) and dead slots are compacted before keys are read. The key-kind check is made once per map. | 1556 → 97 ms; 367 → 167 ms |
|
|
178
|
-
| `any-record-view` | Viewing an `any` object as `Record<string, any>` rebuilt it through reflection on every conversion. | `TnRecordOf` wraps the object's own map in a `TnMap` (writes through the record reach the object, as in JS). | 1696 → 273 ms |
|
|
179
|
-
| `str-concat`, `str-concat-plus` | `s = s + a + b` compiled to Go string concatenation, which copies all of `s`: O(n²) for a growing string. `TnStrAppend` also locked with `defer`. | `s = s + …` on a string variable or field (the target as the leftmost operand) compiles to `TnStrAppend`, like `s += …`, and the lock no longer uses `defer`. | 51.5 → 26.4 ms; 3365 → 101 ms |
|
|
180
|
-
| `str-short-scan` | For strings under 512 bytes, `charCodeAt(i)` rescanned `s[:i+1]` for ASCII and `s.length` rescanned all of `s`, so walking a line was O(n²). | The ASCII answer for the last short string asked about is remembered (the entry holds the string). Indexes under 64 still scan their few bytes. | 300 → 196 ms |
|
|
181
|
-
| `any-dynamic` | Reads of `any` objects missed the field-plan table and went through accessor and reflect checks before reaching the map. Writes boxed every value through `reflect.ValueOf`. | `TnGetRef`/`TnGetPeek` read a `map[string]interface{}` first. `tnBoxArray` returns scalars and maps without reflect. | 3812 → 2501 ms |
|
|
182
|
-
| `num-to-string` | `toFixed` formatted 1100 digits on every call to round exactly. | `strconv` already rounds to nearest. Only a value exactly on a tie (its last binary fraction digit at 2^-(d+1)) takes the exact path. | 667 → 416 ms |
|
|
183
|
-
| `events-emit` | `TnInvoke` called every listener through `reflect.Value.Call`. `emit` copied the listener list and built a `kept` list on every call. | Typed fast paths for `func()`, `func(any)`, `func(any, any)`, `func(...any)`, `func(float64)` and `func(string)`. `emit` uses the copy-on-write listener slice as its snapshot. | 1372 → 658 ms |
|
|
184
|
-
| `regex-site` | Each evaluation of a regex literal looked its compiled program up again (key concatenation and a mutex). | One package-level `TnRegexSite` per literal shares the program and RE2 fallbacks. Each evaluation is still a new `RegExp` with its own `lastIndex`. | 177 → 168 ms |
|
|
185
|
-
| `gen-for-of` (examples) | Generators called through dynamic dispatch (`unit.ops()` on an abstract `CompilationUnit`, 26 phases of angular-templates) still ran as goroutines, and `for…of` over them collected every value first; spread and `Array.from` of a generator received through reflect. | `TnCallSeq_<m>[T]` dispatches a generator method on a dynamic receiver to the class's push form (its channel in lockstep for a generator without one, any other value's elements otherwise), for `for…of`, `yield*` and `super.m()`. A spread or `Array.from` of a generator call collects its push form into a typed slice (`TnGeneratorItems`). | angular-templates at 64 KB: 721 → 601 ms |
|
|
186
|
-
| `gen-for-of` (examples) | `Array.from` / spread over a class iterator reached through a dynamic field (`op.handlerOps`, `op.trackByOps` in angular-templates) found `*[Symbol.iterator]()` with `reflect.MethodByName`, called it, and drained its channel on a goroutine: one reflective call and two goroutine hand-offs per operation list. | `TnToArray` asserts the class's push form directly (`Symbol_iterator__seq`, a `func(func(T) bool)`) and iterates it in place; an interface-yield channel method gets its own type assertion too, without reflection. | angular-templates at 64 KB: 601 → 305 ms |
|
|
187
|
-
| `class-dispatch` | A call on an abstract base class or an interface went through `TnCall_<m>(recv, args ...interface{})`: arguments and the result boxed, then unboxed with `TnAs`. | When every class of the receiver's declared hierarchy declares the method with the same Go signature, the call goes through `TnCall_<C>_<m>(recv, a P0, …) R`: a type switch calling the method with its own types, the boxed dispatcher only for any other receiver. The call's inferred type is `R`, so the value isn't boxed again where it's used. | 709 → 124 ms |
|
|
188
|
-
| `async-await` | Each async call started a goroutine and handed the JS baton over twice, and each settle allocated two wake-up channels even with nobody waiting. | An async function that awaits only calls of other such functions (whose promises are settled when they return), with no `for await` and no promise returned, runs on the caller's goroutine (`TnRunAsyncInline`). `TnAsyncSend` / `TnAsyncClose` allocate a wake-up channel only for a parked awaiter. | 1985 → 201 ms |
|
|
189
|
-
| `json-roundtrip` | `JSON.parse` built a `TnMap` per object from `encoding/json` tokens, then `as Item[]` converted every map to a struct through `TnConvertDynamic`. | `JSON.parse(text)` into a typed slot is `TnJSONParseTyped[T]`: a scanner decodes the text straight into `T` (per-type plans over field offsets) when every object has `T`'s keys in its field order and every value its kind; anything else (other keys, null, any fields, invalid text) decodes and converts as before. | 347 → 97 ms |
|
|
190
|
-
| `num-bitwise` | Loop counters and flag locals were `float64`, so every bitwise operand converted float64 to int32 through `TnInt32`. | A `for` loop's `let` counter (an integer literal start, `++` / `--`, never written in the body) is a Go `int`, and a block `const` holding a bitwise result an `int32`, when every read is an arithmetic, comparison, bitwise, index or template operand: reads are `float64(i)`, bitwise operands take the integer directly. | 128 → 114 ms |
|
|
191
|
-
| `any-record-view` | `TnRecordOf` still listed the object's keys on every view: it ranged the Go map, sorted the keys and scanned each one for an array index (71% of the probe), though a record read by key never needs them. | A view lists its keys the first time something reads them (`Object.keys`, `for…in`, a delete, a new key, `size`). A key added through the `any` object before then is listed too, as in JS. | 263 → 96.5 ms |
|
|
192
|
-
| `arr-push-index` | A regression: single-value pushes became `x = TnPush(x, v)` (2x growth, for yaml-cli), which stores the whole slice header, with a GC write barrier, on every push. `x = append(x, v)` writes only the length while there is spare capacity. | A push onto a variable or field is `TnPushTo(&x, v)`, which keeps the 2x growth and stores the slice only when it grows. A map's value (`m[k]`), spreads and several values keep the assignment. [arr-push-local](arr-push-local.ts) covers short local and field arrays. | 105 → 49.2 ms |
|
|
193
|
-
|
|
194
|
-
### Open
|
|
195
|
-
|
|
196
|
-
| Probe | Cause | Status |
|
|
197
|
-
| ----- | ----- | ------ |
|
|
198
|
-
| `any-dynamic` | What remains is the representation: an `any` object is a Go `map[string]interface{}`, so every property access hashes its key (`memHashAES` and map lookups are about 45%), where V8 reads a slot through a hidden class. | open |
|
|
199
|
-
| `any-record-view` | With the keys listed lazily, what remains is a `TnMap` allocated per view (the constructor is over Go's inlining budget, so it can't stay on the stack) and each value read as `any` and converted with `TnAs`. | open |
|
|
200
|
-
| `events-emit` | Listener shapes other than the fast paths (here `(a: number, b: string)`) still go through `reflect.Value.Call` (about a third), and each `emit` hashes the event name. | open |
|
|
201
|
-
| `str-short-scan`, `str-char-scan` | Indexes under 64 still rescan their prefix for ASCII (`tnStrIsASCII` is 40% of `str-short-scan`). A loop over `s.charCodeAt(i)` could test `s` once before the loop. | open |
|
|
202
|
-
| `num-mod-int`, `num-bitwise` | Integer loop counters are Go `int`s now, but `%` still runs on `float64` (`TnMod` converts both operands and checks them, then divides with `idiv`), and other integral locals (`k = (k + i) % 97`, module constants such as `F_A = 1 << 0`) stay `float64`. A `TnMod` by a constant divisor would let Go multiply instead of divide, but any fallback call puts the helper over Go's inlining budget; it would have to be emitted inline. | open |
|
|
203
|
-
| `class-accessors` | A getter or setter on a receiver whose declared class has subclasses compiles to `TnGetRef(c, "value")` / `TnSet(c, "value", …)`, which finds `Get_value` / `Set_value` with `reflect.MethodByName` and calls it through `reflect.Value.Call` on every access (about 80%). Methods on such receivers already get a typed `TnCall_<C>_<m>`; accessors don't. | open |
|
|
204
|
-
| `obj-keys-entries` | `Object.keys` / `values` / `entries` of a struct rebuild its property list through reflection on every call (`tnStructProps` → `tnCollectStructProps`, about 60%, mostly `reflect.(*rtype).Field`), and box each value. | open |
|
|
205
|
-
| `obj-spread-destructure` | `{ ...base, x }` into a typed object goes through `TnAssignTo` (54%): the source's own properties are collected into a new Go map (`tnOwnProps`), then each is set by name through reflection (`tnAssignFields` → `tnFieldByKey`). | open |
|
|
206
|
-
| `str-unicode` | `s.length` of a short non-ASCII string builds its UTF-16 position index again on each call (`TnStrLength` → `tnStrCount` → `tnStrIndexOf`, about 45%, plus the allocations behind most GC time). The long string's `charCodeAt` reuses its index (16%). | open |
|
|
207
|
-
| `async-promise-all` | `new Promise` resolved from `queueMicrotask`, `Promise.all` and `.then` take the goroutine path (`TnStartAsync`, channel sends, `selectgo`, waking OS threads with `wakep` / `startm`); `TnRunAsyncInline` doesn't apply because `fanOut` awaits `Promise.all`. | open |
|
|
208
|
-
| `num-hash-imul` | `fnv1a` is 84%: each `s.charCodeAt(i)` and `s.length` rechecks the string for ASCII (`tnStrIsASCII` 24%, as in `str-short-scan`), and `h`, a `float64` local, goes through `TnInt32` for every `^=` and `Math.imul` (24%, as in `num-bitwise`). | open |
|
|
209
|
-
| `bigint-arith` | Every `bigint` literal is `TnBigInt("1")`, a `big.Int.SetString` parse, each time it is evaluated, including in `modpow`'s loop condition (`TnBigInt` is 45%), and `e >>= 1n` builds `Neg(TnBigInt("1"))`. `BigInt(i)` parses a string too. | open |
|
|
210
|
-
| `iter-helpers` | Iterator helpers over `Iterator.from(array)` and generators run each stage as a goroutine feeding a channel (`TnIterFrom` / `TnIterDrop` closures, channel send / receive and goroutine wake-ups about 40%), not the push form that `for…of` uses. | open |
|
|
211
|
-
| `wasm-call` | `api.add(…)` on `instance.exports as Api` is `TnCallMember`: a `TnGet` of the export from the exports `TnMap`, then `TnCallValue` with the arguments boxed into a new `[]interface{}` (73% together); wazero's own `CallWithStack` is about 10%. | open |
|
|
212
|
-
| `typed-array` | Every `Int32Array` write converts through `tnTypedConv`, which calls `math.Mod` for ToInt32 (21%) even when the value is in range, the case `TnInt32` already short-cuts. | open |
|
|
213
|
-
| `gen-next` | `.next()`, spread and a stored generator use the goroutine and channel form (channel send / receive and thread wake-ups about 60%); only a direct `for…of` gets the push form. | open |
|
|
214
|
-
| `union-typeof` | `typeof v === 'number'` on a union value calls `TnTypeOf` (62%), which builds the type's name through reflection (`rtype.String`) and compares strings, where a type switch would do. | open |
|
|
215
|
-
| `fn-rest-spread` | `.call`, `.apply` and a bound method go through `TnWithThis` (79%), which copies the arguments into a new `[]interface{}` through reflection (`TnSliceAs`) on every call. | open |
|
|
216
|
-
|
|
217
|
-
Bugs found while writing the probes (not performance, not fixed here):
|
|
218
|
-
`'x' + null` and `'x' + undefined` render `<nil>`, and a `-0` concatenated
|
|
219
|
-
into a string renders `-0e+0` (JS gives `null`, `undefined` and `0`); a
|
|
220
|
-
subclass of a class with `*[Symbol.iterator]()` can't be iterated (the
|
|
221
|
-
generator isn't copied into it); and examples/marked-cli's parity run fails
|
|
222
|
-
69 `--tokens` cases at this commit and before (a `def` token's unmatched
|
|
223
|
-
`title` group prints `""` where Node omits `undefined`).
|
|
224
|
-
|
|
225
|
-
Found while pinning the fixes above (also before them, not fixed here): an
|
|
226
|
-
`await` of a promise that is already settled resumes on the spot, so in
|
|
227
|
-
`const p = f(); log('x')` the part of `f` after its first `await` runs
|
|
228
|
-
before `x` (JS runs it after); a `for…of` over an array doesn't see elements
|
|
229
|
-
pushed during the loop; a class method returning `null` (`next(): Base |
|
|
230
|
-
null`) and a typed field read from JSON `null` compare `=== null` as
|
|
231
|
-
`undefined`, and `await` of a `Promise<void>` gives `{}`; an object literal
|
|
232
|
-
typed as an interface, called through a dynamic receiver, throws `area is
|
|
233
|
-
not a function`; a class whose arrow-function field implements an interface
|
|
234
|
-
method, and `[...shapes]` of a `Base[]` into a `Shape[]`, don't compile;
|
|
235
|
-
`.length` counts a character outside the BMP once (JS counts two UTF-16
|
|
236
|
-
units); `JSON.parse` of a number past the float range (`1e400`) throws
|
|
237
|
-
where JS gives `Infinity`; and a JSON object whose keys come in another
|
|
238
|
-
order than its type's fields loses that order once converted to the type.
|
|
239
|
-
|
|
240
|
-
Found while writing the second batch of probes (not fixed here; each probe
|
|
241
|
-
works around it and says so in a comment): `cur.meta?.tag?.length` on an
|
|
242
|
-
optional `string` field doesn't compile (`*string has no field or method
|
|
243
|
-
length`), so `obj-optional-chain` uses `(cur.meta?.tag ?? '').length`;
|
|
244
|
-
`obj.method.call(other, …)` and `obj.method.bind(other)` ignore `other` and
|
|
245
|
-
keep `obj` as `this` (`fn-rest-spread` passes the same receiver); and
|
|
246
|
-
`Buffer#toString('utf8')` and `TextDecoder` turn a truncated multi-byte
|
|
247
|
-
sequence (`f0 90 41`, `e2 82`) into one U+FFFD per byte where JS gives one
|
|
248
|
-
per maximal subpart (`buf-encode` decodes only valid text).
|
|
@@ -1,21 +0,0 @@
|
|
|
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`);
|
|
@@ -1,20 +0,0 @@
|
|
|
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`);
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// perf: `for…of`, `forEach`, `find` / `findIndex` and `entries()` over arrays of objects
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
interface Row {
|
|
5
|
-
id: number;
|
|
6
|
-
qty: number;
|
|
7
|
-
active: boolean;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const rows: Row[] = [];
|
|
11
|
-
for (let i = 0; i < 2048; i++) rows.push({ id: i, qty: (i * 7) & 63, active: (i & 3) !== 0 });
|
|
12
|
-
|
|
13
|
-
const start = performance.now();
|
|
14
|
-
let total = 0;
|
|
15
|
-
for (let pass = 0; pass < 3000; pass++) {
|
|
16
|
-
for (const r of rows) if (r.active) total += r.qty;
|
|
17
|
-
rows.forEach((r) => {
|
|
18
|
-
total += r.id & 1;
|
|
19
|
-
});
|
|
20
|
-
for (const [i, r] of rows.entries()) total += (i ^ r.qty) & 3;
|
|
21
|
-
const target = (pass * 5) & 2047;
|
|
22
|
-
const hit = rows.find((r) => r.id === target);
|
|
23
|
-
total += hit ? hit.qty : 0;
|
|
24
|
-
total += rows.findIndex((r) => r.qty === (pass & 63));
|
|
25
|
-
}
|
|
26
|
-
const elapsed = performance.now() - start;
|
|
27
|
-
|
|
28
|
-
console.log(`checksum: ${total}`);
|
|
29
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
package/performance/arr-hof.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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`);
|
|
@@ -1,15 +0,0 @@
|
|
|
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`);
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
// perf: building many short arrays with `push` inside functions (locals and fields of new objects)
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
class Line {
|
|
5
|
-
tokens: number[] = [];
|
|
6
|
-
words: string[] = [];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const WORDS = ['let', 'x', '=', 'y', '+', '1', ';'];
|
|
10
|
-
|
|
11
|
-
// A lexer-style list: a local array, pushed into and returned
|
|
12
|
-
function scan(seed: number, count: number): number[] {
|
|
13
|
-
const out: number[] = [];
|
|
14
|
-
for (let i = 0; i < count; i++) out.push((seed + i) & 255);
|
|
15
|
-
return out;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Pushes into the fields of a new object
|
|
19
|
-
function build(seed: number, count: number): Line {
|
|
20
|
-
const line = new Line();
|
|
21
|
-
for (let i = 0; i < count; i++) {
|
|
22
|
-
line.tokens.push((seed ^ i) & 63);
|
|
23
|
-
if ((i & 3) === 0) line.words.push(WORDS[i & 7] ?? '');
|
|
24
|
-
}
|
|
25
|
-
return line;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const start = performance.now();
|
|
29
|
-
let total = 0;
|
|
30
|
-
for (let pass = 0; pass < 1_000_000; pass++) {
|
|
31
|
-
const count = 4 + (pass & 63);
|
|
32
|
-
const a = scan(pass, count);
|
|
33
|
-
const line = build(pass, count);
|
|
34
|
-
total += a.length + a[count - 1] + line.tokens.length + line.tokens[count >> 1] + line.words.length;
|
|
35
|
-
}
|
|
36
|
-
const elapsed = performance.now() - start;
|
|
37
|
-
|
|
38
|
-
console.log(`checksum: ${total}`);
|
|
39
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// perf: `slice`, `splice`, `concat`, `[...a, ...b]`, `includes` and `indexOf` on arrays
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const nums: number[] = [];
|
|
5
|
-
for (let i = 0; i < 64; i++) nums.push((i * 13) & 127);
|
|
6
|
-
const names = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta'];
|
|
7
|
-
const lengths = names.map((s) => s.length);
|
|
8
|
-
|
|
9
|
-
const start = performance.now();
|
|
10
|
-
let total = 0;
|
|
11
|
-
for (let i = 0; i < 450_000; i++) {
|
|
12
|
-
const lo = i & 15;
|
|
13
|
-
const part = nums.slice(lo, lo + 24);
|
|
14
|
-
const joined = part.concat(nums.slice(0, 8));
|
|
15
|
-
const spread = [...part, ...lengths];
|
|
16
|
-
joined.splice(lo, 4, 1, 2);
|
|
17
|
-
total += part.length + joined.length + spread.length + joined[lo];
|
|
18
|
-
if (nums.includes(i & 127)) total++;
|
|
19
|
-
total += nums.indexOf((i * 13) & 127) + names.indexOf(names[i & 7]);
|
|
20
|
-
}
|
|
21
|
-
const elapsed = performance.now() - start;
|
|
22
|
-
|
|
23
|
-
console.log(`checksum: ${total}`);
|
|
24
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
package/performance/arr-sort.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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`);
|
|
@@ -1,24 +0,0 @@
|
|
|
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();
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
// perf: `Promise.all` fan-out, `new Promise` + `.then` chains and `queueMicrotask` (the general async path)
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
function delayed(n: number): Promise<number> {
|
|
5
|
-
return new Promise<number>((resolve) => {
|
|
6
|
-
queueMicrotask(() => resolve(n & 15));
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
async function fanOut(base: number): Promise<number> {
|
|
11
|
-
const parts = await Promise.all([delayed(base), delayed(base + 1), delayed(base + 2), delayed(base + 3)]);
|
|
12
|
-
let s = 0;
|
|
13
|
-
for (const p of parts) s += p;
|
|
14
|
-
return s;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Nested `.then` callbacks (TypeNative types `then` as returning void, so no chaining)
|
|
18
|
-
function chain(n: number): Promise<number> {
|
|
19
|
-
return new Promise<number>((resolve) => {
|
|
20
|
-
Promise.resolve(n).then((x) => {
|
|
21
|
-
Promise.resolve(x + 1).then((y) => resolve((y * 2) & 31));
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async function main(): Promise<void> {
|
|
27
|
-
const start = performance.now();
|
|
28
|
-
let total = 0;
|
|
29
|
-
for (let i = 0; i < 100_000; i++) {
|
|
30
|
-
total += await fanOut(i);
|
|
31
|
-
total += await chain(i);
|
|
32
|
-
}
|
|
33
|
-
const elapsed = performance.now() - start;
|
|
34
|
-
console.log(`checksum: ${total}`);
|
|
35
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
main();
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
// perf: `bigint` `+`, `*`, `%` and shifts (Fibonacci mod a prime, modular exponentiation)
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const P = 1_000_000_007n;
|
|
5
|
-
|
|
6
|
-
function modpow(base: bigint, exp: bigint, mod: bigint): bigint {
|
|
7
|
-
let result = 1n;
|
|
8
|
-
let b = base % mod;
|
|
9
|
-
let e = exp;
|
|
10
|
-
while (e > 0n) {
|
|
11
|
-
if ((e & 1n) === 1n) result = (result * b) % mod;
|
|
12
|
-
b = (b * b) % mod;
|
|
13
|
-
e >>= 1n;
|
|
14
|
-
}
|
|
15
|
-
return result;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const start = performance.now();
|
|
19
|
-
let total = 0n;
|
|
20
|
-
let a = 0n;
|
|
21
|
-
let b = 1n;
|
|
22
|
-
for (let i = 0; i < 1_500_000; i++) {
|
|
23
|
-
const next = (a + b) % P;
|
|
24
|
-
a = b;
|
|
25
|
-
b = next;
|
|
26
|
-
}
|
|
27
|
-
total += b;
|
|
28
|
-
for (let i = 1; i < 100_000; i++) total += modpow(BigInt(i), 65_537n, P);
|
|
29
|
-
// a few large values: 2^400 grows past one machine word
|
|
30
|
-
let big = 1n << 400n;
|
|
31
|
-
for (let i = 0; i < 250_000; i++) big = (big * 3n + BigInt(i)) % (1n << 401n);
|
|
32
|
-
total += big & 0xffffn;
|
|
33
|
-
const elapsed = performance.now() - start;
|
|
34
|
-
|
|
35
|
-
console.log(`checksum: ${total}`);
|
|
36
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// perf: `Buffer` integer reads and writes, `hex` / `base64` / `utf8` conversion, `Buffer.concat`, `Uint8Array` `toHex` / `fromBase64` and `TextEncoder` / `TextDecoder`
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
import { Buffer } from 'node:buffer';
|
|
4
|
-
|
|
5
|
-
const text = 'héllo wörld, packets and frames: 0123456789 '.repeat(4);
|
|
6
|
-
const encoder = new TextEncoder();
|
|
7
|
-
const decoder = new TextDecoder();
|
|
8
|
-
|
|
9
|
-
const start = performance.now();
|
|
10
|
-
let total = 0;
|
|
11
|
-
for (let i = 0; i < 40_000; i++) {
|
|
12
|
-
const buf = Buffer.alloc(64);
|
|
13
|
-
for (let off = 0; off < 64; off += 4) buf.writeUInt32LE((i * 2654435761 + off) >>> 0, off);
|
|
14
|
-
for (let off = 0; off < 64; off += 4) total += buf.readUInt32LE(off) & 255;
|
|
15
|
-
const hex = buf.toString('hex');
|
|
16
|
-
const b64 = buf.toString('base64');
|
|
17
|
-
total += hex.length + b64.length + Buffer.from(b64, 'base64')[i & 63];
|
|
18
|
-
const joined = Buffer.concat([buf, Buffer.from(text)]);
|
|
19
|
-
// (only the text part: TypeNative decodes truncated UTF-8 sequences differently, see the README)
|
|
20
|
-
total += joined.length + joined.toString('utf8', 64).length;
|
|
21
|
-
const u8 = Uint8Array.fromBase64(b64);
|
|
22
|
-
total += u8.toHex().length;
|
|
23
|
-
const enc = encoder.encode(text);
|
|
24
|
-
total += enc.length + decoder.decode(enc).length;
|
|
25
|
-
}
|
|
26
|
-
const elapsed = performance.now() - start;
|
|
27
|
-
|
|
28
|
-
console.log(`checksum: ${total}`);
|
|
29
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
// perf: getters and setters, static fields and `super.m()` calls on class instances
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
class Counter {
|
|
5
|
-
static created = 0;
|
|
6
|
-
private _value = 0;
|
|
7
|
-
constructor() {
|
|
8
|
-
Counter.created++;
|
|
9
|
-
}
|
|
10
|
-
get value(): number {
|
|
11
|
-
return this._value;
|
|
12
|
-
}
|
|
13
|
-
set value(v: number) {
|
|
14
|
-
this._value = v & 0xffff;
|
|
15
|
-
}
|
|
16
|
-
step(n: number): number {
|
|
17
|
-
return n + 1;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
class Doubling extends Counter {
|
|
22
|
-
step(n: number): number {
|
|
23
|
-
return super.step(n) * 2;
|
|
24
|
-
}
|
|
25
|
-
get twice(): number {
|
|
26
|
-
return this.value * 2;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const counters: Counter[] = [];
|
|
31
|
-
for (let i = 0; i < 256; i++) counters.push((i & 1) === 0 ? new Counter() : new Doubling());
|
|
32
|
-
const d = new Doubling();
|
|
33
|
-
|
|
34
|
-
const start = performance.now();
|
|
35
|
-
let total = 0;
|
|
36
|
-
for (let pass = 0; pass < 20_000; pass++) {
|
|
37
|
-
for (let i = 0; i < counters.length; i++) {
|
|
38
|
-
const c = counters[i];
|
|
39
|
-
c.value = c.value + (i & 7);
|
|
40
|
-
total += c.step(i & 3);
|
|
41
|
-
}
|
|
42
|
-
d.value = pass;
|
|
43
|
-
total += d.twice + Counter.created;
|
|
44
|
-
}
|
|
45
|
-
const elapsed = performance.now() - start;
|
|
46
|
-
|
|
47
|
-
console.log(`checksum: ${total}`);
|
|
48
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|