-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.mjs
More file actions
767 lines (627 loc) · 23.4 KB
/
bench.mjs
File metadata and controls
767 lines (627 loc) · 23.4 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
#!/usr/bin/env node
/**
* Sibu — Stress Test / Benchmark
*
* Measures performance of:
* 1. Signal creation (signal)
* 2. Signal read/write throughput
* 3. Computed derivations (derived)
* 4. Effect tracking (effect)
* 5. Watcher propagation (watch)
* 6. Batch updates
* 7. DOM element creation (tagFactory)
* 8. Reactive DOM updates (attribute binding)
* 9. List rendering & diffing (each — LIS reconciliation)
* 10. Deep dependency graph (diamond pattern)
*
* Run: node bench.mjs
*/
import { JSDOM } from "jsdom";
// ── Bootstrap jsdom ──────────────────────────────────────────────────────────
//
// This script is standalone (run via `node bench.mjs`) so mutating globalThis
// here is safe. The installBenchGlobals/restoreBenchGlobals pair is exported
// shape for anyone importing this module from a test runner that cares about
// global cleanup. Note: queueMicrotask is already available natively in Node.
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
const BENCH_GLOBAL_KEYS = ["document", "HTMLElement", "Element", "Node", "Comment"];
const _savedGlobals = {};
export function installBenchGlobals() {
for (const key of BENCH_GLOBAL_KEYS) {
_savedGlobals[key] = Object.prototype.hasOwnProperty.call(globalThis, key)
? globalThis[key]
: undefined;
globalThis[key] = dom.window[key];
}
}
export function restoreBenchGlobals() {
for (const key of BENCH_GLOBAL_KEYS) {
if (_savedGlobals[key] === undefined) {
delete globalThis[key];
} else {
globalThis[key] = _savedGlobals[key];
}
}
}
installBenchGlobals();
// ── Import Sibu (from source via tsup output) ────────────────────────────────
const {
signal,
effect,
derived,
watch,
batch,
div,
span,
button,
ul,
li,
mount,
each,
html,
} = await import("./dist/index.js");
// ── Helpers ──────────────────────────────────────────────────────────────────
const RESET = "\x1b[0m";
const BOLD = "\x1b[1m";
const GREEN = "\x1b[32m";
const CYAN = "\x1b[36m";
const YELLOW = "\x1b[33m";
const DIM = "\x1b[2m";
function fmt(n) {
return n.toLocaleString("en-US");
}
function fmtTime(ms) {
if (ms < 1) return `${(ms * 1000).toFixed(0)} µs`;
if (ms < 1000) return `${ms.toFixed(2)} ms`;
return `${(ms / 1000).toFixed(2)} s`;
}
function fmtOps(ops) {
if (ops >= 1_000_000) return `${(ops / 1_000_000).toFixed(2)}M ops/s`;
if (ops >= 1_000) return `${(ops / 1_000).toFixed(2)}K ops/s`;
return `${ops.toFixed(0)} ops/s`;
}
function runBench(name, fn, { iterations = 1, warmup = 0 } = {}) {
// Warmup
for (let i = 0; i < warmup; i++) fn();
const start = performance.now();
for (let i = 0; i < iterations; i++) fn();
const elapsed = performance.now() - start;
const perOp = elapsed / iterations;
const opsPerSec = (iterations / elapsed) * 1000;
return { name, elapsed, iterations, perOp, opsPerSec };
}
function printResult(r) {
const opsStr = r.iterations > 1 ? ` ${DIM}${fmtOps(r.opsPerSec)}${RESET}` : "";
console.log(
` ${GREEN}✓${RESET} ${r.name.padEnd(44)} ${CYAN}${fmtTime(r.elapsed).padStart(10)}${RESET}` +
` ${DIM}(${fmt(r.iterations)} iters, ${fmtTime(r.perOp)}/op)${RESET}${opsStr}`
);
}
function section(title) {
console.log(`\n${BOLD}${YELLOW}── ${title} ${"─".repeat(58 - title.length)}${RESET}`);
}
// ── Benchmarks ───────────────────────────────────────────────────────────────
console.log(`\n${BOLD}╔══════════════════════════════════════════════════════════════════╗${RESET}`);
console.log(`${BOLD}║ Sibu — Stress Test / Benchmark ║${RESET}`);
console.log(`${BOLD}╚══════════════════════════════════════════════════════════════════╝${RESET}`);
const results = [];
// ─── 1. Signal creation ──────────────────────────────────────────────────────
section("1. Signal Creation (signal)");
const N_SIGNALS = 100_000;
results.push(
runBench(`Create ${fmt(N_SIGNALS)} signals`, () => {
for (let i = 0; i < N_SIGNALS; i++) signal(i);
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
// ─── 2. Signal read/write throughput ─────────────────────────────────────────
section("2. Signal Read / Write Throughput");
const N_RW = 500_000;
{
const [get, set] = signal(0);
results.push(
runBench(`${fmt(N_RW)} reads`, () => {
for (let i = 0; i < N_RW; i++) get();
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(`${fmt(N_RW)} writes (no subscribers)`, () => {
for (let i = 0; i < N_RW; i++) set(i);
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
}
// ─── 3. Computed derivations ─────────────────────────────────────────────────
section("3. Computed Derivations (derived)");
{
const N_COMPUTED = 10_000;
const [get, set] = signal(1);
results.push(
runBench(`Create ${fmt(N_COMPUTED)} computed from 1 signal`, () => {
for (let i = 0; i < N_COMPUTED; i++) derived(() => get() * 2);
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
// Chain of computed values
const CHAIN_LEN = 1_000;
const [cGet, cSet] = signal(0);
let prev = cGet;
for (let i = 0; i < CHAIN_LEN; i++) {
const p = prev;
prev = derived(() => p() + 1);
}
const chainEnd = prev;
results.push(
runBench(`Propagate through ${fmt(CHAIN_LEN)}-deep computed chain`, () => {
for (let i = 0; i < 1000; i++) {
cSet(i);
chainEnd(); // force evaluation
}
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
}
// ─── 4. Effect tracking ─────────────────────────────────────────────────────
section("4. Effect Tracking (effect)");
{
const N_EFFECTS = 10_000;
const [get, set] = signal(0);
let effectCount = 0;
const cleanups = [];
results.push(
runBench(`Create ${fmt(N_EFFECTS)} effects on 1 signal`, () => {
for (let i = 0; i < N_EFFECTS; i++) {
cleanups.push(effect(() => { get(); effectCount++; }));
}
}, { iterations: 1 })
);
printResult(results.at(-1));
// Update signal → triggers all effects
effectCount = 0;
results.push(
runBench(`Trigger ${fmt(N_EFFECTS)} effects (1 signal update)`, () => {
set((v) => v + 1);
}, { iterations: 100, warmup: 0 })
);
printResult(results.at(-1));
console.log(` ${DIM}(${fmt(effectCount)} total effect runs)${RESET}`);
// Cleanup
for (const c of cleanups) c();
}
// ─── 5. Watcher propagation ─────────────────────────────────────────────────
section("5. Watcher Propagation (watch)");
{
const N_WATCHERS = 5_000;
const [get, set] = signal(0);
let watchCount = 0;
const teardowns = [];
for (let i = 0; i < N_WATCHERS; i++) {
teardowns.push(watch(get, () => { watchCount++; }));
}
results.push(
runBench(`Notify ${fmt(N_WATCHERS)} watchers per update`, () => {
set((v) => v + 1);
}, { iterations: 200, warmup: 5 })
);
printResult(results.at(-1));
console.log(` ${DIM}(${fmt(watchCount)} total watcher calls)${RESET}`);
for (const t of teardowns) t();
}
// ─── 6. Batch updates ───────────────────────────────────────────────────────
section("6. Batch Updates");
{
const N_BATCH = 1_000;
const signals = [];
for (let i = 0; i < N_BATCH; i++) signals.push(signal(0));
let effectRuns = 0;
const cleanup = effect(() => {
for (const [get] of signals) get();
effectRuns++;
});
// Without batch — each signal change triggers the effect separately
effectRuns = 0;
results.push(
runBench(`${fmt(N_BATCH)} updates WITHOUT batch`, () => {
for (let i = 0; i < N_BATCH; i++) signals[i][1]((v) => v + 1);
}, { iterations: 10, warmup: 1 })
);
printResult(results.at(-1));
console.log(` ${DIM}(${fmt(effectRuns)} effect runs)${RESET}`);
// With batch — all updates coalesced, effect fires once per batch
effectRuns = 0;
results.push(
runBench(`${fmt(N_BATCH)} updates WITH batch`, () => {
batch(() => {
for (let i = 0; i < N_BATCH; i++) signals[i][1]((v) => v + 1);
});
}, { iterations: 10, warmup: 1 })
);
printResult(results.at(-1));
console.log(` ${DIM}(${fmt(effectRuns)} effect runs — expected 11: 1 warmup + 10 iters)${RESET}`);
cleanup();
}
// ─── 7. DOM element creation ─────────────────────────────────────────────────
section("7. DOM Element Creation (tagFactory)");
{
const N_ELEMENTS = 10_000;
results.push(
runBench(`Create ${fmt(N_ELEMENTS)} <div> elements (no props)`, () => {
for (let i = 0; i < N_ELEMENTS; i++) div();
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(`Create ${fmt(N_ELEMENTS)} <div> with props`, () => {
for (let i = 0; i < N_ELEMENTS; i++) {
div({
id: `item-${i}`,
class: "card active",
style: { color: "red", fontSize: "14px" },
"data-index": String(i),
});
}
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(`Create ${fmt(N_ELEMENTS)} nested trees (div > span + span)`, () => {
for (let i = 0; i < N_ELEMENTS; i++) {
div({ nodes: [span({ nodes: ["Hello"] }), span({ nodes: ["World"] })] });
}
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
}
// ─── 7b. Three Authoring Styles Compared ────────────────────────────────────
section("7b. Three Authoring Styles Compared");
{
const N = 10_000;
// ── Test 1: Simple element (no props) ──
console.log(`\n ${DIM}── Simple <div> (no props) ──${RESET}`);
results.push(
runBench(` props obj: ${fmt(N)} <div>`, () => {
for (let i = 0; i < N; i++) div({});
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(` shorthand: ${fmt(N)} <div>`, () => {
for (let i = 0; i < N; i++) div();
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(` html tmpl: ${fmt(N)} <div>`, () => {
for (let i = 0; i < N; i++) html`<div></div>`;
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
// ── Test 2: Element with class + text ──
console.log(`\n ${DIM}── <p> with class + text ──${RESET}`);
results.push(
runBench(` props obj: ${fmt(N)} <p class+text>`, () => {
for (let i = 0; i < N; i++) {
span({ class: "label", nodes: "Hello" });
}
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(` shorthand: ${fmt(N)} <p class+text>`, () => {
for (let i = 0; i < N; i++) {
span("label", "Hello");
}
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(` html tmpl: ${fmt(N)} <p class+text>`, () => {
for (let i = 0; i < N; i++) {
html`<span class="label">Hello</span>`;
}
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
// ── Test 3: Element with dynamic attributes ──
console.log(`\n ${DIM}── <div> with dynamic attrs ──${RESET}`);
results.push(
runBench(` props obj: ${fmt(N)} <div> dyn attrs`, () => {
for (let i = 0; i < N; i++) {
div({ id: `item-${i}`, class: "card active", "data-index": String(i) });
}
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(` shorthand: ${fmt(N)} <div> dyn attrs`, () => {
for (let i = 0; i < N; i++) {
div({ id: `item-${i}`, class: "card active", "data-index": String(i) });
}
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
results.push(
runBench(` html tmpl: ${fmt(N)} <div> dyn attrs`, () => {
for (let i = 0; i < N; i++) {
html`<div id=${`item-${i}`} class="card active" data-index=${String(i)}></div>`;
}
}, { iterations: 10, warmup: 2 })
);
printResult(results.at(-1));
// ── Test 4: Nested tree (div > span + span) ──
console.log(`\n ${DIM}── Nested tree (div > span + span) ──${RESET}`);
results.push(
runBench(` props obj: ${fmt(N)} nested`, () => {
for (let i = 0; i < N; i++) {
div({ nodes: [span({ nodes: "Hello" }), span({ nodes: "World" })] });
}
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
results.push(
runBench(` shorthand: ${fmt(N)} nested`, () => {
for (let i = 0; i < N; i++) {
div([span("Hello"), span("World")]);
}
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
results.push(
runBench(` html tmpl: ${fmt(N)} nested`, () => {
for (let i = 0; i < N; i++) {
html`<div><span>Hello</span><span>World</span></div>`;
}
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
// ── Test 5: Deep nesting (3 levels with event handler) ──
console.log(`\n ${DIM}── Deep nesting (div > div > button with event) ──${RESET}`);
const handler = () => {};
results.push(
runBench(` props obj: ${fmt(N)} deep+event`, () => {
for (let i = 0; i < N; i++) {
div({ class: "card", nodes: [
div({ class: "body", nodes: [
span({ nodes: `Item ${i}` }),
button({ nodes: "Click", class: "btn", on: { click: handler } }),
] }),
] });
}
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
results.push(
runBench(` shorthand: ${fmt(N)} deep+event`, () => {
for (let i = 0; i < N; i++) {
div("card", [
div("body", [
span(`Item ${i}`),
button({ nodes: "Click", class: "btn", on: { click: handler } }),
]),
]);
}
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
results.push(
runBench(` html tmpl: ${fmt(N)} deep+event`, () => {
for (let i = 0; i < N; i++) {
html`<div class="card">
<div class="body">
<span>${`Item ${i}`}</span>
<button class="btn" on:click=${handler}>Click</button>
</div>
</div>`;
}
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
}
// ─── 8. Reactive DOM updates ─────────────────────────────────────────────────
section("8. Reactive DOM Updates");
{
const container = document.createElement("div");
document.body.appendChild(container);
const [getText, setText] = signal("initial");
const [getCls, setCls] = signal("off");
const el = div({
class: getCls,
nodes: [() => getText()],
});
mount(el, container);
// Flush microtasks for initial binding
await new Promise((r) => setTimeout(r, 10));
const N_UPDATES = 10_000;
results.push(
runBench(`${fmt(N_UPDATES)} reactive class updates`, () => {
for (let i = 0; i < N_UPDATES; i++) setCls(i % 2 === 0 ? "on" : "off");
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
container.innerHTML = "";
}
// ─── 9. List rendering & diffing (each) ──────────────────────────────────────
section("9. List Rendering & Diffing (each)");
{
const container = document.createElement("div");
document.body.appendChild(container);
const [getItems, setItems] = signal(
Array.from({ length: 1000 }, (_, i) => ({ id: i, label: `Item ${i}` }))
);
const list = ul({
nodes: [
each(
getItems,
(item) => li({ nodes: [item.label] }),
{ key: (item) => item.id }
),
],
});
mount(list, container);
await new Promise((r) => setTimeout(r, 10));
// Append items
results.push(
runBench("Append 1,000 items to 1,000-item list", () => {
setItems((prev) => [
...prev.slice(0, 1000),
...Array.from({ length: 1000 }, (_, i) => ({
id: 10000 + i + Math.random() * 100000,
label: `New ${i}`,
})),
]);
}, { iterations: 50, warmup: 2 })
);
printResult(results.at(-1));
// Reverse the list
setItems(Array.from({ length: 1000 }, (_, i) => ({ id: i, label: `Item ${i}` })));
await new Promise((r) => setTimeout(r, 10));
results.push(
runBench("Reverse 1,000-item list", () => {
setItems((prev) => [...prev].reverse());
}, { iterations: 100, warmup: 5 })
);
printResult(results.at(-1));
// Remove every other item
setItems(Array.from({ length: 2000 }, (_, i) => ({ id: i + 50000, label: `Item ${i}` })));
await new Promise((r) => setTimeout(r, 10));
results.push(
runBench("Remove every 2nd item from 2,000-item list", () => {
setItems((prev) => prev.filter((_, i) => i % 2 === 0));
}, { iterations: 50, warmup: 2 })
);
printResult(results.at(-1));
// Shuffle
setItems(Array.from({ length: 1000 }, (_, i) => ({ id: i + 90000, label: `Item ${i}` })));
await new Promise((r) => setTimeout(r, 10));
results.push(
runBench("Shuffle 1,000-item list (random reorder)", () => {
setItems((prev) => {
const copy = [...prev];
for (let i = copy.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[copy[i], copy[j]] = [copy[j], copy[i]];
}
return copy;
});
}, { iterations: 100, warmup: 5 })
);
printResult(results.at(-1));
// Clear the entire list
setItems(Array.from({ length: 5000 }, (_, i) => ({ id: i + 200000, label: `Item ${i}` })));
await new Promise((r) => setTimeout(r, 10));
results.push(
runBench("Clear 5,000-item list", () => {
setItems([]);
}, { iterations: 50, warmup: 2 })
);
printResult(results.at(-1));
container.innerHTML = "";
}
// ─── 10. Deep diamond dependency graph ───────────────────────────────────────
section("10. Deep Diamond Dependency Graph");
{
// [A]
// / \
// [B] [C]
// \ /
// [D] ← should update once per A change
const [getA, setA] = signal(0);
const getB = derived(() => getA() + 1);
const getC = derived(() => getA() * 2);
const getD = derived(() => getB() + getC());
let dUpdates = 0;
const cleanup = effect(() => {
getD();
dUpdates++;
});
dUpdates = 0;
results.push(
runBench("Diamond graph: 10,000 root updates", () => {
for (let i = 0; i < 10_000; i++) setA(i);
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
console.log(` ${DIM}(${fmt(dUpdates)} D-effect runs for ${fmt(50_000)} A updates)${RESET}`);
// Wide diamond: N leaves depending on 1 root, 1 aggregator depending on all
const WIDTH = 500;
const [rootGet, rootSet] = signal(0);
const leaves = [];
for (let i = 0; i < WIDTH; i++) {
leaves.push(derived(() => rootGet() + i));
}
const aggregator = derived(() => {
let sum = 0;
for (const l of leaves) sum += l();
return sum;
});
let aggUpdates = 0;
const cleanup2 = effect(() => {
aggregator();
aggUpdates++;
});
aggUpdates = 0;
results.push(
runBench(`Wide diamond (${WIDTH} branches): 1,000 root updates`, () => {
for (let i = 0; i < 1_000; i++) rootSet(i);
}, { iterations: 5, warmup: 1 })
);
printResult(results.at(-1));
console.log(` ${DIM}(${fmt(aggUpdates)} aggregator runs)${RESET}`);
cleanup();
cleanup2();
}
// ─── Summary ─────────────────────────────────────────────────────────────────
console.log(`\n${BOLD}${YELLOW}── Summary ${"─".repeat(54)}${RESET}`);
const totalMs = results.reduce((s, r) => s + r.elapsed, 0);
console.log(` Total: ${CYAN}${fmtTime(totalMs)}${RESET} (${results.length} benchmarks)\n`);
// ─── JSON output & baseline comparison ───────────────────────────────────────
import { readFileSync, writeFileSync, existsSync } from "node:fs";
const BASELINE_FILE = "bench-baseline.json";
const jsonFlag = process.argv.includes("--json");
const saveFlag = process.argv.includes("--save");
const compareFlag = process.argv.includes("--compare");
const jsonResults = results.map(r => ({
name: r.name.trim(),
elapsed: Math.round(r.elapsed * 100) / 100,
iterations: r.iterations,
perOp: Math.round(r.perOp * 1000) / 1000,
opsPerSec: Math.round(r.opsPerSec),
}));
if (jsonFlag) {
console.log(JSON.stringify(jsonResults, null, 2));
}
if (saveFlag) {
writeFileSync(BASELINE_FILE, JSON.stringify(jsonResults, null, 2));
console.log(`${GREEN}✓${RESET} Baseline saved to ${BASELINE_FILE}`);
}
if (compareFlag && existsSync(BASELINE_FILE)) {
const baseline = JSON.parse(readFileSync(BASELINE_FILE, "utf-8"));
const RED = "\x1b[31m";
console.log(`\n${BOLD}${YELLOW}── Regression Check ${"─".repeat(45)}${RESET}`);
let regressions = 0;
for (const current of jsonResults) {
const base = baseline.find(b => b.name === current.name);
if (!base) continue;
const ratio = current.perOp / base.perOp;
const pctChange = ((ratio - 1) * 100).toFixed(1);
if (ratio > 1.2) {
// >20% slower = regression
console.log(` ${RED}✗${RESET} ${current.name.padEnd(44)} ${RED}+${pctChange}% slower${RESET} (${fmtTime(base.perOp)} → ${fmtTime(current.perOp)})`);
regressions++;
} else if (ratio < 0.8) {
// >20% faster = improvement
console.log(` ${GREEN}↑${RESET} ${current.name.padEnd(44)} ${GREEN}${pctChange}% faster${RESET}`);
} else {
console.log(` ${DIM}≈ ${current.name.padEnd(44)} ${pctChange}%${RESET}`);
}
}
if (regressions > 0) {
console.log(`\n ${RED}${BOLD}${regressions} regression(s) detected!${RESET} (>20% slower than baseline)\n`);
process.exitCode = 1;
} else {
console.log(`\n ${GREEN}${BOLD}No regressions detected.${RESET}\n`);
}
} else if (compareFlag) {
console.log(`\n${YELLOW}No baseline found.${RESET} Run with --save first to create one.\n`);
}