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/obj-alloc.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
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`);
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// perf: `Object.keys` / `values` / `entries` and `for…in` over typed objects and class instances
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
interface Config {
|
|
5
|
-
width: number;
|
|
6
|
-
height: number;
|
|
7
|
-
depth: number;
|
|
8
|
-
scale: number;
|
|
9
|
-
offset: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
class Point {
|
|
13
|
-
x: number;
|
|
14
|
-
y: number;
|
|
15
|
-
z: number;
|
|
16
|
-
constructor(x: number, y: number, z: number) {
|
|
17
|
-
this.x = x;
|
|
18
|
-
this.y = y;
|
|
19
|
-
this.z = z;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const configs: Config[] = [];
|
|
24
|
-
const points: Point[] = [];
|
|
25
|
-
for (let i = 0; i < 256; i++) {
|
|
26
|
-
configs.push({ width: i, height: i + 1, depth: i & 7, scale: 2, offset: i >> 2 });
|
|
27
|
-
points.push(new Point(i, i * 2, i * 3));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const start = performance.now();
|
|
31
|
-
let total = 0;
|
|
32
|
-
for (let pass = 0; pass < 2400; pass++) {
|
|
33
|
-
for (let i = 0; i < configs.length; i++) {
|
|
34
|
-
const c = configs[i];
|
|
35
|
-
total += Object.keys(c).length;
|
|
36
|
-
for (const v of Object.values(c)) total += v;
|
|
37
|
-
for (const [k, v] of Object.entries(points[i])) total += k.length + v;
|
|
38
|
-
for (const k in c) total += k.length;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
const elapsed = performance.now() - start;
|
|
42
|
-
|
|
43
|
-
console.log(`checksum: ${total}`);
|
|
44
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
// perf: `?.` and `??` walking nullable links and optional fields
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
interface Meta {
|
|
5
|
-
weight?: number;
|
|
6
|
-
tag?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
class Node {
|
|
10
|
-
value: number;
|
|
11
|
-
next: Node | null = null;
|
|
12
|
-
meta?: Meta;
|
|
13
|
-
constructor(value: number) {
|
|
14
|
-
this.value = value;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
let head: Node | null = null;
|
|
19
|
-
for (let i = 0; i < 4096; i++) {
|
|
20
|
-
const n = new Node(i);
|
|
21
|
-
if ((i & 3) === 0) n.meta = { weight: i & 15 };
|
|
22
|
-
else if ((i & 3) === 1) n.meta = { tag: 't' };
|
|
23
|
-
n.next = head;
|
|
24
|
-
head = n;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const start = performance.now();
|
|
28
|
-
let total = 0;
|
|
29
|
-
for (let pass = 0; pass < 2800; pass++) {
|
|
30
|
-
let cur: Node | null = head;
|
|
31
|
-
while (cur) {
|
|
32
|
-
total += cur.meta?.weight ?? 1;
|
|
33
|
-
// (not `cur.meta?.tag?.length ?? 0`: that doesn't compile yet, see the README)
|
|
34
|
-
total += (cur.meta?.tag ?? '').length;
|
|
35
|
-
total += cur.next?.next?.value ?? 0;
|
|
36
|
-
cur = cur.next;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
const elapsed = performance.now() - start;
|
|
40
|
-
|
|
41
|
-
console.log(`checksum: ${total}`);
|
|
42
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// perf: a `Record<string, number>` object literal used as a dictionary (`rec[k] ?? 0`, `k in rec`, `Object.keys`)
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const WORDS: string[] = [];
|
|
5
|
-
for (let i = 0; i < 512; i++) WORDS.push(`w${(i * 7919) & 255}`);
|
|
6
|
-
|
|
7
|
-
const start = performance.now();
|
|
8
|
-
let total = 0;
|
|
9
|
-
for (let pass = 0; pass < 4000; pass++) {
|
|
10
|
-
const counts: Record<string, number> = {};
|
|
11
|
-
for (let i = 0; i < WORDS.length; i++) {
|
|
12
|
-
const w = WORDS[(i + pass) & 511];
|
|
13
|
-
counts[w] = (counts[w] ?? 0) + 1;
|
|
14
|
-
}
|
|
15
|
-
if ('w7' in counts) total += counts['w7'];
|
|
16
|
-
if (!('missing' in counts)) total += 1;
|
|
17
|
-
const keys = Object.keys(counts);
|
|
18
|
-
total += keys.length;
|
|
19
|
-
for (let k = 0; k < keys.length; k += 16) total += counts[keys[k]];
|
|
20
|
-
}
|
|
21
|
-
const elapsed = performance.now() - start;
|
|
22
|
-
|
|
23
|
-
console.log(`checksum: ${total}`);
|
|
24
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// perf: object spread `{ ...p, x }`, destructuring with defaults, and array-destructuring swaps
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
interface Vec {
|
|
5
|
-
x: number;
|
|
6
|
-
y: number;
|
|
7
|
-
z: number;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface Options {
|
|
11
|
-
size?: number;
|
|
12
|
-
depth?: number;
|
|
13
|
-
label?: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function area({ size = 4, depth = 2, label = 'n' }: Options): number {
|
|
17
|
-
return size * depth + label.length;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const base: Vec = { x: 1, y: 2, z: 3 };
|
|
21
|
-
const opts: Options[] = [{}, { size: 3 }, { depth: 5, label: 'abc' }, { size: 1, depth: 1, label: '' }];
|
|
22
|
-
|
|
23
|
-
const start = performance.now();
|
|
24
|
-
let total = 0;
|
|
25
|
-
let a = 1;
|
|
26
|
-
let b = 2;
|
|
27
|
-
for (let i = 0; i < 4_500_000; i++) {
|
|
28
|
-
const moved: Vec = { ...base, x: i & 255 };
|
|
29
|
-
const { x, y, z } = moved;
|
|
30
|
-
total += x + y + z;
|
|
31
|
-
total += area(opts[i & 3]);
|
|
32
|
-
[a, b] = [b, a + (i & 1)];
|
|
33
|
-
const [first, second] = [x, y];
|
|
34
|
-
total += first - second;
|
|
35
|
-
}
|
|
36
|
-
total += a + b;
|
|
37
|
-
const elapsed = performance.now() - start;
|
|
38
|
-
|
|
39
|
-
console.log(`checksum: ${total}`);
|
|
40
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,29 +0,0 @@
|
|
|
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`);
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// perf: `replace` with a callback, `matchAll` with groups and `split` on a regex
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const lines: string[] = [];
|
|
5
|
-
for (let i = 0; i < 512; i++) lines.push(`user=${i} role=r${i & 7}; size=${i * 3}, flag=${(i & 1) === 0}`);
|
|
6
|
-
|
|
7
|
-
const PAIR = /(\w+)=(\w+)/g;
|
|
8
|
-
const SEP = /[;,]\s*/;
|
|
9
|
-
|
|
10
|
-
const start = performance.now();
|
|
11
|
-
let total = 0;
|
|
12
|
-
for (let pass = 0; pass < 150; pass++) {
|
|
13
|
-
for (let i = 0; i < lines.length; i++) {
|
|
14
|
-
const s = lines[i];
|
|
15
|
-
const upper = s.replace(PAIR, (_m: string, k: string, v: string) => `${k.toUpperCase()}:${v.length}`);
|
|
16
|
-
total += upper.length;
|
|
17
|
-
for (const m of s.matchAll(PAIR)) total += m[1].length + (m.index ?? 0);
|
|
18
|
-
total += s.split(SEP).length;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
const elapsed = performance.now() - start;
|
|
22
|
-
|
|
23
|
-
console.log(`checksum: ${total}`);
|
|
24
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,28 +0,0 @@
|
|
|
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`);
|
package/performance/set-ops.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
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`);
|
|
@@ -1,35 +0,0 @@
|
|
|
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`);
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// perf: growing strings with `s = s + x` (not `+=`), each pass starting empty
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const passes = 1000;
|
|
5
|
-
|
|
6
|
-
const start = performance.now();
|
|
7
|
-
let total = 0;
|
|
8
|
-
for (let pass = 0; pass < passes; pass++) {
|
|
9
|
-
let lines = '';
|
|
10
|
-
for (let i = 0; i < 3000; i++) {
|
|
11
|
-
lines = lines + 'line ' + i + '\n';
|
|
12
|
-
}
|
|
13
|
-
total += lines.length;
|
|
14
|
-
}
|
|
15
|
-
const elapsed = performance.now() - start;
|
|
16
|
-
|
|
17
|
-
console.log(`checksum: ${total}`);
|
|
18
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// perf: growing ~20 KB strings with `s += x` and `s = s + …` (each pass starts empty)
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const passes = 400;
|
|
5
|
-
const words = ['alpha', 'beta', 'gamma', 'delta'];
|
|
6
|
-
|
|
7
|
-
const start = performance.now();
|
|
8
|
-
let total = 0;
|
|
9
|
-
for (let pass = 0; pass < passes; pass++) {
|
|
10
|
-
let out = '';
|
|
11
|
-
for (let i = 0; i < 3000; i++) {
|
|
12
|
-
out += words[i & 3];
|
|
13
|
-
out += ' ';
|
|
14
|
-
}
|
|
15
|
-
let lines = '';
|
|
16
|
-
for (let i = 0; i < 300; i++) {
|
|
17
|
-
lines = lines + 'line ' + i + '\n';
|
|
18
|
-
}
|
|
19
|
-
total += out.length + lines.length;
|
|
20
|
-
}
|
|
21
|
-
const elapsed = performance.now() - start;
|
|
22
|
-
|
|
23
|
-
console.log(`checksum: ${total}`);
|
|
24
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// perf: `startsWith` / `endsWith` / `includes`, case conversion, `padStart`, `repeat` and `replaceAll` with a string
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const lines: string[] = [];
|
|
5
|
-
for (let i = 0; i < 256; i++) lines.push(`GET /api/v${i & 3}/items/${i}?Sort=Name&Page=${i >> 4} HTTP/1.1`);
|
|
6
|
-
|
|
7
|
-
const start = performance.now();
|
|
8
|
-
let total = 0;
|
|
9
|
-
for (let pass = 0; pass < 2500; pass++) {
|
|
10
|
-
for (let i = 0; i < lines.length; i += 2) {
|
|
11
|
-
const s = lines[i];
|
|
12
|
-
if (s.startsWith('GET ')) total++;
|
|
13
|
-
if (s.endsWith('1.1')) total++;
|
|
14
|
-
if (s.includes('/v2/')) total += 2;
|
|
15
|
-
total += s.toLowerCase().length + s.toUpperCase().charCodeAt(5);
|
|
16
|
-
total += String(i).padStart(6, '0').length;
|
|
17
|
-
total += s.replaceAll('/', '|').length;
|
|
18
|
-
if ((i & 63) === 0) total += '-'.repeat(i >> 2).length;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
const elapsed = performance.now() - start;
|
|
22
|
-
|
|
23
|
-
console.log(`checksum: ${total}`);
|
|
24
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// perf: `charCodeAt(i)` / `s[i]` scans of many short (< 512 byte) lines
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const lines: string[] = [];
|
|
5
|
-
for (let i = 0; i < 2000; i++) {
|
|
6
|
-
lines.push(` key_${i}: value ${i * 3} "quoted text" # a comment that pads the line out a bit more ${i}`);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const start = performance.now();
|
|
10
|
-
let idents = 0;
|
|
11
|
-
let digits = 0;
|
|
12
|
-
let quotes = 0;
|
|
13
|
-
for (let pass = 0; pass < 100; pass++) {
|
|
14
|
-
for (const line of lines) {
|
|
15
|
-
for (let i = 0; i < line.length; i++) {
|
|
16
|
-
const c = line.charCodeAt(i);
|
|
17
|
-
if ((c >= 97 && c <= 122) || c === 95) idents++;
|
|
18
|
-
else if (c >= 48 && c <= 57) digits++;
|
|
19
|
-
else if (line[i] === '"') quotes++;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
const elapsed = performance.now() - start;
|
|
24
|
-
|
|
25
|
-
console.log(`checksum: ${idents * 1000000 + digits * 1000 + quotes}`);
|
|
26
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// perf: `split`, `join`, `indexOf`, `slice` and `trim` on CSV-like lines
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const lines: string[] = [];
|
|
5
|
-
for (let i = 0; i < 1000; i++) lines.push(` id${i}, name ${i}, ${i * 7}, tag-${i % 17}, done `);
|
|
6
|
-
const csv = lines.join('\n');
|
|
7
|
-
|
|
8
|
-
const start = performance.now();
|
|
9
|
-
let sum = 0;
|
|
10
|
-
for (let pass = 0; pass < 200; pass++) {
|
|
11
|
-
const rows = csv.split('\n');
|
|
12
|
-
for (const row of rows) {
|
|
13
|
-
const cells = row.split(',');
|
|
14
|
-
sum += cells.length;
|
|
15
|
-
const tag = cells[3].trim();
|
|
16
|
-
sum += tag.slice(tag.indexOf('-') + 1).length;
|
|
17
|
-
sum += cells.map((c) => c.trim()).join('|').length;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
const elapsed = performance.now() - start;
|
|
21
|
-
|
|
22
|
-
console.log(`checksum: ${sum}`);
|
|
23
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
// perf: `switch` over string values and `===` / `<` string comparisons (keyword lookup)
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const SOURCE = 'function let const if else return while for of in class new this x y value';
|
|
5
|
-
const words = SOURCE.split(' ');
|
|
6
|
-
|
|
7
|
-
function keyword(w: string): number {
|
|
8
|
-
switch (w) {
|
|
9
|
-
case 'function':
|
|
10
|
-
return 1;
|
|
11
|
-
case 'let':
|
|
12
|
-
case 'const':
|
|
13
|
-
return 2;
|
|
14
|
-
case 'if':
|
|
15
|
-
return 3;
|
|
16
|
-
case 'else':
|
|
17
|
-
return 4;
|
|
18
|
-
case 'return':
|
|
19
|
-
return 5;
|
|
20
|
-
case 'while':
|
|
21
|
-
case 'for':
|
|
22
|
-
return 6;
|
|
23
|
-
case 'of':
|
|
24
|
-
case 'in':
|
|
25
|
-
return 7;
|
|
26
|
-
case 'class':
|
|
27
|
-
return 8;
|
|
28
|
-
case 'new':
|
|
29
|
-
return 9;
|
|
30
|
-
case 'this':
|
|
31
|
-
return 10;
|
|
32
|
-
default:
|
|
33
|
-
return 0;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const start = performance.now();
|
|
38
|
-
let total = 0;
|
|
39
|
-
for (let pass = 0; pass < 1_200_000; pass++) {
|
|
40
|
-
for (let i = 0; i < words.length; i++) {
|
|
41
|
-
const w = words[i];
|
|
42
|
-
total += keyword(w);
|
|
43
|
-
if (w === 'value') total++;
|
|
44
|
-
if (w < words[(i + 1) & 15]) total++;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
const elapsed = performance.now() - start;
|
|
48
|
-
|
|
49
|
-
console.log(`checksum: ${total}`);
|
|
50
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// perf: template literals mixing strings, integers, floats and booleans
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const n = 1_000_000;
|
|
5
|
-
const names = ['ada', 'grace', 'linus', 'ken'];
|
|
6
|
-
|
|
7
|
-
const start = performance.now();
|
|
8
|
-
let total = 0;
|
|
9
|
-
for (let i = 0; i < n; i++) {
|
|
10
|
-
const s = `${names[i & 3]}#${i}: ${i / 4} items, ok=${(i & 1) === 0}`;
|
|
11
|
-
total += s.length;
|
|
12
|
-
}
|
|
13
|
-
const elapsed = performance.now() - start;
|
|
14
|
-
|
|
15
|
-
console.log(`checksum: ${total}`);
|
|
16
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// perf: `length`, `charCodeAt`, `codePointAt` and `slice` on non-ASCII text (UTF-16 indexes)
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const parts = ['naïve café', 'Straße', 'Ελληνικά', 'добрый день', '日本語のテキスト', 'smörgåsbord'];
|
|
5
|
-
const lines: string[] = [];
|
|
6
|
-
for (let i = 0; i < 400; i++) lines.push(`${parts[i & 3]} #${i} ${parts[(i >> 2) & 3]} ${parts[4 + (i & 1)]}`);
|
|
7
|
-
const big = lines.join('\n');
|
|
8
|
-
|
|
9
|
-
const start = performance.now();
|
|
10
|
-
let total = 0;
|
|
11
|
-
for (let pass = 0; pass < 8400; pass++) {
|
|
12
|
-
// a long string scanned by index
|
|
13
|
-
for (let i = 0; i < big.length; i += 3) total += big.charCodeAt(i) & 255;
|
|
14
|
-
// many short lines: length, codePointAt, slice
|
|
15
|
-
for (let j = 0; j < lines.length; j++) {
|
|
16
|
-
const s = lines[j];
|
|
17
|
-
total += s.length;
|
|
18
|
-
total += (s.codePointAt(s.length >> 1) ?? 0) & 127;
|
|
19
|
-
total += s.slice(2, 9).length;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
const elapsed = performance.now() - start;
|
|
23
|
-
|
|
24
|
-
console.log(`checksum: ${total}`);
|
|
25
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
// perf: `Float64Array` / `Int32Array` / `Uint8Array` indexed loops, `subarray`, `set` and `fill`
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
const N = 4096;
|
|
5
|
-
const f = new Float64Array(N);
|
|
6
|
-
const ints = new Int32Array(N);
|
|
7
|
-
const bytes = new Uint8Array(N);
|
|
8
|
-
for (let i = 0; i < N; i++) {
|
|
9
|
-
f[i] = i * 0.5;
|
|
10
|
-
ints[i] = i * 3;
|
|
11
|
-
bytes[i] = i & 255;
|
|
12
|
-
}
|
|
13
|
-
const scratch = new Int32Array(256);
|
|
14
|
-
|
|
15
|
-
const start = performance.now();
|
|
16
|
-
let total = 0;
|
|
17
|
-
for (let pass = 0; pass < 6000; pass++) {
|
|
18
|
-
let s = 0;
|
|
19
|
-
for (let i = 0; i < N; i++) s += f[i] * 2;
|
|
20
|
-
for (let i = 1; i < N; i++) ints[i] = (ints[i - 1] + ints[i]) & 0xffff;
|
|
21
|
-
for (let i = 0; i < N; i++) bytes[i] = (bytes[i] + 1) & 255;
|
|
22
|
-
const view = ints.subarray(pass & 1023, (pass & 1023) + 256);
|
|
23
|
-
scratch.set(view);
|
|
24
|
-
scratch.fill(pass & 7, 0, 16);
|
|
25
|
-
total += s + scratch[17] + scratch[0] + bytes[pass & 4095];
|
|
26
|
-
}
|
|
27
|
-
const elapsed = performance.now() - start;
|
|
28
|
-
|
|
29
|
-
console.log(`checksum: ${total}`);
|
|
30
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
// perf: evaluating an expression tree of a discriminated union with `switch (e.kind)`
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
interface Num {
|
|
5
|
-
kind: 'num';
|
|
6
|
-
value: number;
|
|
7
|
-
}
|
|
8
|
-
interface Add {
|
|
9
|
-
kind: 'add';
|
|
10
|
-
left: Expr;
|
|
11
|
-
right: Expr;
|
|
12
|
-
}
|
|
13
|
-
interface Mul {
|
|
14
|
-
kind: 'mul';
|
|
15
|
-
left: Expr;
|
|
16
|
-
right: Expr;
|
|
17
|
-
}
|
|
18
|
-
interface Neg {
|
|
19
|
-
kind: 'neg';
|
|
20
|
-
operand: Expr;
|
|
21
|
-
}
|
|
22
|
-
type Expr = Num | Add | Mul | Neg;
|
|
23
|
-
|
|
24
|
-
function build(depth: number, seed: number): Expr {
|
|
25
|
-
if (depth === 0) return { kind: 'num', value: (seed % 10) + 1 };
|
|
26
|
-
switch (seed % 3) {
|
|
27
|
-
case 0:
|
|
28
|
-
return { kind: 'add', left: build(depth - 1, seed * 3 + 1), right: build(depth - 1, seed + 7) };
|
|
29
|
-
case 1:
|
|
30
|
-
return { kind: 'mul', left: build(depth - 1, seed + 2), right: build(depth - 1, seed * 7 + 3) };
|
|
31
|
-
default:
|
|
32
|
-
return { kind: 'neg', operand: build(depth - 1, seed + 5) };
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function evaluate(e: Expr): number {
|
|
37
|
-
switch (e.kind) {
|
|
38
|
-
case 'num':
|
|
39
|
-
return e.value;
|
|
40
|
-
case 'add':
|
|
41
|
-
return (evaluate(e.left) + evaluate(e.right)) % 1009;
|
|
42
|
-
case 'mul':
|
|
43
|
-
return ((evaluate(e.left) * evaluate(e.right)) % 1009) + 1;
|
|
44
|
-
case 'neg':
|
|
45
|
-
return 1010 - evaluate(e.operand);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const tree = build(18, 1);
|
|
50
|
-
|
|
51
|
-
const start = performance.now();
|
|
52
|
-
let total = 0;
|
|
53
|
-
for (let i = 0; i < 20; i++) total += evaluate(tree);
|
|
54
|
-
const elapsed = performance.now() - start;
|
|
55
|
-
|
|
56
|
-
console.log(`checksum: ${total}`);
|
|
57
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// perf: `typeof`, `instanceof` and `Array.isArray` narrowing of a five-way union (string, number, boolean, class, array)
|
|
2
|
-
import { performance } from 'node:perf_hooks';
|
|
3
|
-
|
|
4
|
-
class Box {
|
|
5
|
-
inner: number;
|
|
6
|
-
constructor(inner: number) {
|
|
7
|
-
this.inner = inner;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
type Value = string | number | boolean | Box | number[];
|
|
12
|
-
|
|
13
|
-
function weigh(v: Value): number {
|
|
14
|
-
if (typeof v === 'number') return v & 15;
|
|
15
|
-
if (typeof v === 'string') return v.length;
|
|
16
|
-
if (typeof v === 'boolean') return v ? 1 : 0;
|
|
17
|
-
if (v instanceof Box) return v.inner;
|
|
18
|
-
if (Array.isArray(v)) return v.length;
|
|
19
|
-
return -1;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const values: Value[] = [];
|
|
23
|
-
for (let i = 0; i < 1024; i++) {
|
|
24
|
-
const kind = i & 7;
|
|
25
|
-
if (kind < 2) values.push(i);
|
|
26
|
-
else if (kind < 4) values.push(`s${i}`);
|
|
27
|
-
else if (kind === 4) values.push((i & 8) !== 0);
|
|
28
|
-
else if (kind < 7) values.push(new Box(i & 63));
|
|
29
|
-
else values.push([i, i]);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const start = performance.now();
|
|
33
|
-
let total = 0;
|
|
34
|
-
for (let pass = 0; pass < 24000; pass++) {
|
|
35
|
-
for (let i = 0; i < values.length; i++) total += weigh(values[i]);
|
|
36
|
-
}
|
|
37
|
-
const elapsed = performance.now() - start;
|
|
38
|
-
|
|
39
|
-
console.log(`checksum: ${total}`);
|
|
40
|
-
console.log(`elapsed: ${elapsed.toFixed(2)} ms`);
|