-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperry-bench-hidden-class-scaling.ts
More file actions
59 lines (55 loc) · 1.98 KB
/
perry-bench-hidden-class-scaling.ts
File metadata and controls
59 lines (55 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Show how per-row cost scales with column count — if the bottleneck
// is the linear keys_array scan inside js_object_set_field_by_name,
// per-row time grows O(N²) with property count (N properties each
// requires scanning 0..N-1 existing keys). A hidden-class / inline-
// cache design is O(N).
function nowMs(): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const g = globalThis as any;
if (g.performance !== undefined && typeof g.performance.now === 'function') {
return g.performance.now();
}
return Date.now();
}
const NAMES: string[] = [];
for (let i = 0; i < 80; i++) NAMES.push('c' + i);
function build(nrows: number, ncols: number): number {
// Warmup
for (let w = 0; w < 3; w++) {
const out: Record<string, unknown>[] = new Array(nrows);
for (let i = 0; i < nrows; i++) {
const o: Record<string, unknown> = {};
for (let j = 0; j < ncols; j++) {
o[NAMES[j]] = i * j;
}
out[i] = o;
}
if (out.length !== nrows) throw new Error('bad');
}
const ITERS = 5;
const t0 = nowMs();
for (let w = 0; w < ITERS; w++) {
const out: Record<string, unknown>[] = new Array(nrows);
for (let i = 0; i < nrows; i++) {
const o: Record<string, unknown> = {};
for (let j = 0; j < ncols; j++) {
o[NAMES[j]] = i * j;
}
out[i] = o;
}
if (out.length !== nrows) throw new Error('bad');
}
return (nowMs() - t0) / ITERS;
}
// Hold row count constant at 10000; sweep column count.
const COLS = [5, 10, 20, 40, 80];
for (let k = 0; k < COLS.length; k++) {
const ncols = COLS[k];
const t = build(10000, ncols);
const perCell = (t * 1000) / (10000 * ncols);
console.log(
'cols=' + String(ncols).padStart(2)
+ ' total=' + t.toFixed(2).padStart(7) + ' ms'
+ ' per-cell=' + perCell.toFixed(2).padStart(5) + ' µs'
);
}