reflex-quant 1.1.2 → 1.3.0
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/README.md +568 -104
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.development.cjs +242 -125
- package/dist/index.development.cjs.map +3 -3
- package/dist/index.development.js +242 -125
- package/dist/index.development.js.map +3 -3
- package/dist/index.js +2 -2
- package/dist/index.js.map +3 -3
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -1,125 +1,561 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
frozen contract (semantic invariants and structural bounds, see
|
|
5
|
-
[CONTRACT.md](CONTRACT.md)), and measure what is left. The contract was extracted
|
|
6
|
-
from the executable spec of [Reflex](https://github.com/volynetstyle/Reflex)
|
|
7
|
-
(provenance in [research/REPORT.md](research/REPORT.md)); it does not follow
|
|
8
|
-
later changes of Reflex.
|
|
3
|
+
# ⚛ reflex-quant
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
machine (graph, tracking, propagation and validation); the **host** handles
|
|
12
|
-
watcher lifecycle and delivery; and the **surface** exposes the typed API.
|
|
13
|
-
Research instrumentation and development diagnostics are eliminated from the
|
|
14
|
-
production build. The standalone ESM bundle is about 2.2 KB gzip.
|
|
5
|
+
**A tiny, high-performance JavaScript signals library built for exact reactive semantics.**
|
|
15
6
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
-
|
|
19
|
-
|
|
20
|
-
same semantics without the engineering (recursive push and pull, one
|
|
21
|
-
exact index, no counters), written as a guided tour. It passes the same
|
|
22
|
-
differential corpus; this branch is its lowering for speed and stack depth.
|
|
7
|
+
[](https://www.npmjs.com/package/reflex-quant)
|
|
8
|
+
[](https://github.com/volynetstyle/reflex-quant/actions/workflows/ci.yml)
|
|
9
|
+
[](#api-at-a-glance)
|
|
10
|
+
[](LICENSE)
|
|
23
11
|
|
|
24
|
-
|
|
12
|
+
[**Website & live examples**](https://volynetstyle.github.io/reflex-quant/) ·
|
|
13
|
+
[How it works](https://volynetstyle.github.io/reflex-quant/reactive-runtime-anatomy/) ·
|
|
14
|
+
[Benchmarks](https://volynetstyle.github.io/reflex-quant/benchmarks/)
|
|
25
15
|
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
Reflex Quant is a framework-agnostic fine-grained reactivity library for JavaScript
|
|
19
|
+
and TypeScript, with signals, computeds and effects in under 3 KB gzip. It combines
|
|
20
|
+
exact dynamic dependency tracking, glitch-free propagation, stack-safe graph traversal
|
|
21
|
+
and defined failure semantics. Its implementation was derived from a written
|
|
22
|
+
reactive contract and validated through differential testing.
|
|
23
|
+
|
|
24
|
+
In the [published JS Reactivity Benchmark snapshot](https://github.com/volynetstyle/js-reactivity-benchmark/blob/9a26a3bee200fe9610e19cc21e0b8e02730b5459/index.html)
|
|
25
|
+
from November 10, 2026, Quant 1.1.2 ranked first on the family-balanced core
|
|
26
|
+
score: 1.07× versus 1.20× for alien-signals 3.2.1. This is a result for that
|
|
27
|
+
workload set, not a claim that Quant wins every benchmark. The separate
|
|
28
|
+
[project CI suite](#performance) measures Quant 1.2.0.
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { signal, computed, effect, get, set } from "reflex-quant";
|
|
32
|
+
|
|
33
|
+
const count = signal(0);
|
|
34
|
+
const doubled = computed(() => get(count) * 2);
|
|
35
|
+
|
|
36
|
+
effect(() => console.log(`count=${get(count)} doubled=${get(doubled)}`));
|
|
37
|
+
// count=0 doubled=0
|
|
38
|
+
|
|
39
|
+
set(count, 1); // count=1 doubled=2
|
|
40
|
+
set(count, 2); // count=2 doubled=4
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
That covers most of it. You store values in **signals**, derive values with **computeds**, and react
|
|
44
|
+
to changes with **effects**. Quant tracks what each one reads, so you never
|
|
45
|
+
list dependencies by hand.
|
|
46
|
+
|
|
47
|
+
## Why quant?
|
|
48
|
+
|
|
49
|
+
- 🤏 **Tiny.** About 2.9 KB min+gzip for the whole library, and no dependencies.
|
|
50
|
+
- 🎯 **Exact.** Dependencies are whatever the last run actually read. Take a
|
|
51
|
+
branch and the old dependencies drop away. Write a value that nothing
|
|
52
|
+
currently reads and nothing reruns.
|
|
53
|
+
- ⚡ **Fast.** Comparable to the fastest signal libraries on the
|
|
54
|
+
[benchmarks](#performance), and reordering dependencies allocates nothing.
|
|
55
|
+
- 🧱 **Stack-safe.** Updates travel through an explicit work stack instead of
|
|
56
|
+
recursion, so a 100 000-deep chain of computeds updates without overflowing
|
|
57
|
+
the call stack.
|
|
58
|
+
- 🔬 **Proven, not guessed.** Every mechanism in the runtime is backed by a test
|
|
59
|
+
that fails without it. Quant's invariants also found
|
|
60
|
+
[two real bugs in Vue's reactivity](#correctness).
|
|
61
|
+
- 🧩 **Framework-free.** Plain functions over plain values. You can use it in
|
|
62
|
+
Node, the browser, workers, or behind any UI layer.
|
|
63
|
+
- 🟦 **TypeScript-first.** Signals, computeds and effects are distinct types:
|
|
64
|
+
the compiler won't let you `set` a computed.
|
|
65
|
+
|
|
66
|
+
## Install
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
npm install reflex-quant
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
ESM and CommonJS builds are included, along with type declarations. It needs Node.js 18+ or any modern browser.
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## The 5-minute tour
|
|
77
|
+
|
|
78
|
+
### 1. Signals hold values
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { signal, get, set, update } from "reflex-quant";
|
|
82
|
+
|
|
83
|
+
const name = signal("Ada");
|
|
84
|
+
|
|
85
|
+
get(name); // "Ada"
|
|
86
|
+
set(name, "Grace"); // replace
|
|
87
|
+
update(name, (n) => n.toUpperCase()); // derive from current: "GRACE"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 2. Computeds derive values, lazily and cached
|
|
91
|
+
|
|
92
|
+
A computed doesn't run until something reads it. It reruns only when a value it
|
|
93
|
+
read has changed.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const price = signal(4);
|
|
97
|
+
const qty = signal(2);
|
|
98
|
+
|
|
99
|
+
let runs = 0;
|
|
100
|
+
const total = computed(() => {
|
|
101
|
+
runs++;
|
|
102
|
+
return get(price) * get(qty);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
set(qty, 3);
|
|
106
|
+
set(qty, 5); // runs === 0: nobody asked yet
|
|
107
|
+
|
|
108
|
+
get(total); // 20, runs === 1
|
|
109
|
+
get(total); // 20, runs === 1: cached
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 3. Effects react to changes
|
|
113
|
+
|
|
114
|
+
An effect runs once right away, then again whenever something it read changes.
|
|
115
|
+
If it returns a function, quant calls that function before the next run and on
|
|
116
|
+
dispose. This is the place to clean up timers, listeners and subscriptions.
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { effect, dispose } from "reflex-quant";
|
|
120
|
+
|
|
121
|
+
const room = signal("general");
|
|
122
|
+
|
|
123
|
+
const chat = effect(() => {
|
|
124
|
+
const r = get(room);
|
|
125
|
+
console.log("join", r);
|
|
126
|
+
return () => console.log("leave", r);
|
|
127
|
+
});
|
|
128
|
+
// join general
|
|
129
|
+
|
|
130
|
+
set(room, "random");
|
|
131
|
+
// leave general
|
|
132
|
+
// join random
|
|
133
|
+
|
|
134
|
+
dispose(chat);
|
|
135
|
+
// leave random
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 4. Dependencies follow your code
|
|
139
|
+
|
|
140
|
+
Quant doesn't use dependency arrays. Each run records exactly what it read,
|
|
141
|
+
so the dependencies change along with your `if` statements:
|
|
28
142
|
|
|
29
143
|
```ts
|
|
30
|
-
|
|
144
|
+
const showFull = signal(false);
|
|
145
|
+
const first = signal("Ada");
|
|
146
|
+
const last = signal("Lovelace");
|
|
147
|
+
|
|
148
|
+
const displayName = computed(() =>
|
|
149
|
+
get(showFull) ? `${get(first)} ${get(last)}` : get(first),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
effect(() => console.log(get(displayName))); // "Ada"
|
|
153
|
+
|
|
154
|
+
set(last, "Byron"); // (nothing: `last` isn't being read right now)
|
|
155
|
+
set(showFull, true); // "Ada Byron"
|
|
156
|
+
set(showFull, false); // "Ada"
|
|
157
|
+
set(last, "King"); // (nothing again)
|
|
158
|
+
```
|
|
31
159
|
|
|
32
|
-
|
|
160
|
+
### 5. No glitches, no duplicate runs
|
|
161
|
+
|
|
162
|
+
When two computeds share a source, the effect still runs **once**, and it
|
|
163
|
+
never sees a half-updated state:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
33
166
|
const first = signal("Ada");
|
|
34
167
|
const last = signal("Lovelace");
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
set(
|
|
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
|
-
|
|
168
|
+
const upper = computed(() => get(first).toUpperCase());
|
|
169
|
+
const initials = computed(() => get(first)[0] + get(last)[0]);
|
|
170
|
+
|
|
171
|
+
effect(() => console.log(`${get(upper)} (${get(initials)})`));
|
|
172
|
+
// ADA (AL)
|
|
173
|
+
|
|
174
|
+
set(first, "Grace");
|
|
175
|
+
// GRACE (GL) (one run, both values already fresh)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 6. Unchanged results stop propagation
|
|
179
|
+
|
|
180
|
+
If a computed recalculates and gets the same value, everything downstream stays
|
|
181
|
+
put:
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
const n = signal(1);
|
|
185
|
+
const parity = computed(() => (get(n) % 2 === 0 ? "even" : "odd"));
|
|
186
|
+
|
|
187
|
+
effect(() => console.log(get(parity))); // "odd"
|
|
188
|
+
|
|
189
|
+
set(n, 3); // (nothing: still "odd")
|
|
190
|
+
set(n, 4); // "even"
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Writing the same value to a signal is also a no-op (compared with `Object.is`).
|
|
194
|
+
|
|
195
|
+
### 7. Batch several writes into one update
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { batch } from "reflex-quant";
|
|
199
|
+
|
|
200
|
+
const x = signal(0);
|
|
201
|
+
const y = signal(0);
|
|
202
|
+
effect(() => console.log(`(${get(x)}, ${get(y)})`)); // (0, 0)
|
|
203
|
+
|
|
204
|
+
batch(() => {
|
|
205
|
+
set(x, 10);
|
|
206
|
+
set(y, 20);
|
|
207
|
+
}); // (10, 20) (one run, not two)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### 8. Read without subscribing
|
|
211
|
+
|
|
212
|
+
`peek` gives you an up-to-date value without making it a dependency.
|
|
213
|
+
`untracked` does the same for a whole block of code.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { peek, untracked } from "reflex-quant";
|
|
217
|
+
|
|
218
|
+
const query = signal("cats");
|
|
219
|
+
const page = signal(1);
|
|
220
|
+
|
|
221
|
+
effect(() => console.log(`search "${get(query)}" page ${peek(page)}`));
|
|
222
|
+
// search "cats" page 1
|
|
223
|
+
|
|
224
|
+
set(page, 2); // (nothing: page was only peeked)
|
|
225
|
+
set(query, "dogs"); // search "dogs" page 2
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## Recipes
|
|
231
|
+
|
|
232
|
+
<details open>
|
|
233
|
+
<summary><b>🛒 Shopping cart</b></summary>
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
const items = signal([
|
|
237
|
+
{ name: "Coffee", price: 4, qty: 2 },
|
|
238
|
+
{ name: "Bagel", price: 3, qty: 1 },
|
|
239
|
+
]);
|
|
240
|
+
const discount = signal(0);
|
|
241
|
+
|
|
242
|
+
const subtotal = computed(() =>
|
|
243
|
+
get(items).reduce((sum, i) => sum + i.price * i.qty, 0),
|
|
244
|
+
);
|
|
245
|
+
const total = computed(() => get(subtotal) * (1 - get(discount)));
|
|
246
|
+
|
|
247
|
+
effect(() => console.log(`Total: $${get(total).toFixed(2)}`));
|
|
248
|
+
// Total: $11.00
|
|
249
|
+
|
|
250
|
+
update(items, (list) => [...list, { name: "Juice", price: 5, qty: 1 }]);
|
|
251
|
+
// Total: $16.00
|
|
252
|
+
|
|
253
|
+
set(discount, 0.1);
|
|
254
|
+
// Total: $14.40
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
</details>
|
|
258
|
+
|
|
259
|
+
<details>
|
|
260
|
+
<summary><b>✅ Todo list with a filter</b></summary>
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
const todos = signal([
|
|
264
|
+
{ text: "Write README", done: true },
|
|
265
|
+
{ text: "Ship 1.2.0", done: false },
|
|
266
|
+
]);
|
|
267
|
+
const filter = signal<"all" | "open" | "done">("all");
|
|
268
|
+
|
|
269
|
+
const visible = computed(() => {
|
|
270
|
+
const f = get(filter);
|
|
271
|
+
if (f === "all") return get(todos);
|
|
272
|
+
return get(todos).filter((t) => t.done === (f === "done"));
|
|
273
|
+
});
|
|
274
|
+
const left = computed(() => get(todos).filter((t) => !t.done).length);
|
|
275
|
+
|
|
276
|
+
effect(() =>
|
|
277
|
+
console.log(`${get(visible).map((t) => t.text).join(", ")} | ${get(left)} left`),
|
|
278
|
+
);
|
|
279
|
+
// Write README, Ship 1.2.0 | 1 left
|
|
280
|
+
|
|
281
|
+
set(filter, "open");
|
|
282
|
+
// Ship 1.2.0 | 1 left
|
|
283
|
+
|
|
284
|
+
update(todos, (list) => list.map((t) => ({ ...t, done: true })));
|
|
285
|
+
// | 0 left
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
</details>
|
|
289
|
+
|
|
290
|
+
<details>
|
|
291
|
+
<summary><b>⏱ Debounced search</b> (with effect cleanup)</summary>
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
const query = signal("");
|
|
295
|
+
|
|
296
|
+
effect(() => {
|
|
297
|
+
const q = get(query);
|
|
298
|
+
const id = setTimeout(() => fetchResults(q), 300);
|
|
299
|
+
return () => clearTimeout(id); // a new keystroke cancels the previous timer
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
set(query, "r");
|
|
303
|
+
set(query, "re");
|
|
304
|
+
set(query, "reflex"); // only "reflex" is fetched
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
</details>
|
|
308
|
+
|
|
309
|
+
<details>
|
|
310
|
+
<summary><b>🖱 Binding to the DOM</b> (no framework)</summary>
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
const count = signal(0);
|
|
314
|
+
const button = document.querySelector("button")!;
|
|
315
|
+
|
|
316
|
+
button.addEventListener("click", () => update(count, (n) => n + 1));
|
|
317
|
+
|
|
318
|
+
effect(() => {
|
|
319
|
+
button.textContent = `Clicked ${get(count)} times`;
|
|
320
|
+
});
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
</details>
|
|
324
|
+
|
|
325
|
+
<details>
|
|
326
|
+
<summary><b>💾 Persisting to localStorage</b></summary>
|
|
327
|
+
|
|
328
|
+
```ts
|
|
329
|
+
const theme = signal(localStorage.getItem("theme") ?? "light");
|
|
330
|
+
|
|
331
|
+
effect(() => localStorage.setItem("theme", get(theme)));
|
|
332
|
+
|
|
333
|
+
set(theme, "dark"); // saved
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
</details>
|
|
337
|
+
|
|
338
|
+
<details>
|
|
339
|
+
<summary><b>🎛 Custom equality</b> (skip updates that don't matter)</summary>
|
|
340
|
+
|
|
341
|
+
A computed receives its previous value. If you return that same object,
|
|
342
|
+
dependents treat the result as unchanged and don't rerun, so you don't need a
|
|
343
|
+
separate comparator option:
|
|
344
|
+
|
|
345
|
+
```ts
|
|
346
|
+
type Cell = { col: number; row: number };
|
|
347
|
+
const mouse = signal({ x: 0, y: 0 });
|
|
348
|
+
|
|
349
|
+
const cell = computed((prev: Cell | undefined) => {
|
|
350
|
+
const { x, y } = get(mouse);
|
|
351
|
+
const next = { col: Math.floor(x / 100), row: Math.floor(y / 100) };
|
|
352
|
+
return prev && prev.col === next.col && prev.row === next.row ? prev : next;
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
effect(() => console.log(get(cell))); // { col: 0, row: 0 }
|
|
356
|
+
|
|
357
|
+
set(mouse, { x: 10, y: 20 }); // (nothing: same cell)
|
|
358
|
+
set(mouse, { x: 150, y: 20 }); // { col: 1, row: 0 }
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
</details>
|
|
362
|
+
|
|
363
|
+
---
|
|
364
|
+
|
|
365
|
+
## API at a glance
|
|
366
|
+
|
|
367
|
+
| Function | What it does |
|
|
368
|
+
| ------------------------ | --------------------------------------------------------------------------------------------- |
|
|
369
|
+
| `signal(value)` | Create a writable value. |
|
|
370
|
+
| `computed(fn, initial?)` | Create a lazy, cached derived value. `fn(prev)` receives the last committed value. |
|
|
371
|
+
| `effect(fn)` | Run `fn` now and again on every change it read. `fn` may return a cleanup function. Returns a handle. |
|
|
372
|
+
| `get(x)` | Read a signal or computed and **subscribe** the current computation to it. |
|
|
373
|
+
| `peek(x)` | Read the up-to-date value **without** subscribing. |
|
|
374
|
+
| `set(s, value)` | Write a signal. Ignored if `Object.is(old, value)`. |
|
|
375
|
+
| `update(s, fn)` | `set(s, fn(current))`. `fn` runs untracked. If it throws, nothing is written. |
|
|
376
|
+
| `batch(fn)` | Run `fn`, then deliver all its writes as a single update. Returns `fn`'s result. |
|
|
377
|
+
| `untracked(fn)` | Run `fn` without subscribing to anything it reads. |
|
|
378
|
+
| `dispose(x)` | Stop an effect (running its cleanup) or detach a computed (its last committed value stays readable). |
|
|
379
|
+
| `typeOf(x)` | `"signal"`, `"computed"` or `"effect"`, without evaluating anything. |
|
|
380
|
+
|
|
381
|
+
> **Why `get(count)` and not `count()` or `count.value`?** Here the handle *is*
|
|
382
|
+
> the reactive node. Quant allocates no wrapper object or getter closure, so
|
|
383
|
+
> each signal costs one object.
|
|
384
|
+
|
|
385
|
+
## Good to know
|
|
386
|
+
|
|
387
|
+
**Replace values, don't mutate them.** Quant compares values with `Object.is`.
|
|
388
|
+
If you push into the same array, the array reference doesn't change, so nothing
|
|
389
|
+
updates:
|
|
390
|
+
|
|
391
|
+
```ts
|
|
392
|
+
update(todos, (list) => { list.push("eggs"); return list; }); // ❌ no update
|
|
393
|
+
update(todos, (list) => [...list, "eggs"]); // ✅ updates
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
**`prev` advances once per run, not once per write.** Computeds are lazy, so
|
|
397
|
+
several writes made before anyone reads collapse into a single run:
|
|
398
|
+
|
|
399
|
+
```ts
|
|
400
|
+
const clicks = signal(0);
|
|
401
|
+
const history = computed((prev) => [...prev, get(clicks)], [] as number[]);
|
|
402
|
+
|
|
403
|
+
set(clicks, 1);
|
|
404
|
+
set(clicks, 2);
|
|
405
|
+
get(history); // [2] (1 was never observed)
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
If you need every write, append to the log in the code that does the writing,
|
|
409
|
+
not in a computed.
|
|
410
|
+
|
|
411
|
+
**Errors don't break the graph.** If a computed throws, `get` rethrows the error
|
|
412
|
+
and the computed keeps its last good value as `prev`. Once its inputs are fixed,
|
|
413
|
+
it recovers on the next read. If an effect throws during creation, it leaves
|
|
414
|
+
nothing running.
|
|
415
|
+
|
|
416
|
+
The stack-safety guarantee covers Quant's graph walks: a chain of 100,000
|
|
417
|
+
computeds is fine. It does not cover callbacks that call each other until
|
|
418
|
+
the native JavaScript stack runs out. That is a resource failure of the
|
|
419
|
+
host, and Quant promises nothing after a program catches it and goes on.
|
|
420
|
+
An error a callback throws itself is an ordinary failure, handled as above.
|
|
421
|
+
|
|
422
|
+
**Disposing from inside a run is safe.** A computed or effect may be
|
|
423
|
+
disposed while it runs, by its own callback or by anything that callback
|
|
424
|
+
calls. The run finishes, then the node detaches and never runs again. A
|
|
425
|
+
computed's run that returns still commits its value, so the read that
|
|
426
|
+
triggered it sees the new value and that value stays readable. If the run
|
|
427
|
+
throws instead, the previous value stays. An effect disposed during its run
|
|
428
|
+
drops the cleanup that run returns: disposal has already run the previous
|
|
429
|
+
one, and nothing will run the new one.
|
|
430
|
+
|
|
431
|
+
**Use the development build to catch cycles.** Bundlers that resolve the
|
|
432
|
+
`development` export condition (Vite, webpack, and Node with
|
|
433
|
+
`--conditions=development`) load a build that throws `Cycle detected` when a
|
|
434
|
+
computed ends up reading itself. The production build skips this check and
|
|
435
|
+
costs nothing extra.
|
|
436
|
+
|
|
437
|
+
## When to use something else
|
|
438
|
+
|
|
439
|
+
Quant is deliberately a **core**: values, derivations, reactions. It doesn't
|
|
440
|
+
include:
|
|
441
|
+
|
|
442
|
+
- deep reactive objects or proxies (`store.user.name = …`),
|
|
443
|
+
- async resources or suspense,
|
|
444
|
+
- component lifecycles or ownership scopes (`effectScope` and similar),
|
|
445
|
+
- a renderer.
|
|
446
|
+
|
|
447
|
+
Each of these can be built on top of quant. None of them is included, because
|
|
448
|
+
the [contract](CONTRACT.md) doesn't require them. If you need them out of the box, a
|
|
449
|
+
full framework (Vue, Solid, Preact Signals, MobX) is a better fit.
|
|
450
|
+
|
|
451
|
+
---
|
|
452
|
+
|
|
453
|
+
## Performance
|
|
454
|
+
|
|
455
|
+
The broader [JS Reactivity Benchmark](https://volynetstyle.github.io/js-reactivity-benchmark/),
|
|
456
|
+
maintained by Quant's author, compares 30 reactivity implementations. Its
|
|
457
|
+
[November 10, 2026 snapshot](https://github.com/volynetstyle/js-reactivity-benchmark/blob/9a26a3bee200fe9610e19cc21e0b8e02730b5459/index.html)
|
|
458
|
+
ranked Quant 1.1.2 first with a **1.07×** family-balanced core score, versus
|
|
459
|
+
**1.20×** for alien-signals 3.2.1. The score covers 33 shared core scenarios;
|
|
460
|
+
stress and lifecycle capability tests are reported separately. Lower is better,
|
|
461
|
+
and the scores are not the same as the pairwise time ratios below. No benchmark
|
|
462
|
+
establishes a universally fastest library.
|
|
463
|
+
|
|
464
|
+
The following is a separate, targeted snapshot of the newer Quant 1.2.0:
|
|
465
|
+
|
|
466
|
+
Measured on CI against [Reflex](https://github.com/volynetstyle/Reflex) and
|
|
467
|
+
[alien-signals](https://github.com/stackblitz/alien-signals), with all three
|
|
468
|
+
runtimes on the same runner
|
|
469
|
+
([run 43479214912](https://github.com/volynetstyle/reflex-quant/actions/runs/43479214912), quant 1.2.0):
|
|
470
|
+
|
|
471
|
+
- within 25 % of the faster of the two on **all 18** scenarios, and faster than both on **7** (plus a tie on
|
|
472
|
+
`fan-out.effects-1000`, 0.998× the faster of the two);
|
|
473
|
+
- geometric mean: **0.94×** the faster of the two per scenario, **0.92×** Reflex,
|
|
474
|
+
**0.43×** alien-signals (lower is faster);
|
|
475
|
+
- reorders allocate nothing. If a computation reads the same 256 sources in a
|
|
476
|
+
rotated order on every pass, quant creates and removes **0** links per pass
|
|
477
|
+
where alien-signals relinks 255 (`node bench/structure.mjs`), and takes 0.53×
|
|
478
|
+
Reflex's time and 0.07× alien-signals' there;
|
|
479
|
+
- Reflex is still faster on reversals, random permutations and sorts
|
|
480
|
+
(`reorder.reverse-8` 1.13×, `value-sort.256x32` 1.19×, `long-reorder.256`
|
|
481
|
+
1.20×) and on the trivial read paths (`static.read-clean` 1.10×, the stable
|
|
482
|
+
wide reads 1.13–1.17×); alien-signals is faster on `static.read-clean` (1.22×)
|
|
483
|
+
and `lifecycle.mount-100` (1.10×).
|
|
484
|
+
|
|
485
|
+
The suite is this project's own. Per-scenario numbers are on the
|
|
486
|
+
[benchmarks page](https://volynetstyle.github.io/reflex-quant/benchmarks/), and the
|
|
487
|
+
trajectory over CI runs is in [research/HISTORY.md](research/HISTORY.md).
|
|
488
|
+
|
|
489
|
+
## Correctness
|
|
490
|
+
|
|
491
|
+
Quant is a research-driven library. Its design asks: what is the smallest known
|
|
492
|
+
reactive runtime that satisfies a fixed contract? The contract is a set of semantic invariants and structural bounds in
|
|
493
|
+
[CONTRACT.md](CONTRACT.md). It was extracted from the executable spec of
|
|
494
|
+
[Reflex](https://github.com/volynetstyle/Reflex)
|
|
495
|
+
(provenance in [research/REPORT.md](research/REPORT.md)). A mechanism is added
|
|
496
|
+
only if dropping it would break the contract or the asymptotics. The ablations
|
|
497
|
+
justify the mechanisms in this implementation; they do not prove a universal
|
|
498
|
+
lower bound for every possible design.
|
|
499
|
+
|
|
500
|
+
- **It found real bugs in Vue.** A port of quant's invariants to Vue 3.6 exposed
|
|
501
|
+
two regressions in its Alien-derived reactivity core, both also reproducible
|
|
502
|
+
in `alien-signals` 3.2.0. One was a lost update
|
|
503
|
+
([vuejs/core#16174](https://github.com/vuejs/core/issues/16174)). The other was
|
|
504
|
+
generational-GC retention through removed links
|
|
505
|
+
([vuejs/core#16176](https://github.com/vuejs/core/issues/16176)). Both are
|
|
506
|
+
now fixed upstream with permanent regression tests
|
|
507
|
+
([#16210](https://github.com/vuejs/core/pull/16210),
|
|
508
|
+
[#16211](https://github.com/vuejs/core/pull/16211)).
|
|
509
|
+
- **Differentially tested.** Quant gives the same answers as Reflex's executable
|
|
510
|
+
spec, with three documented choices applied, on all 27 061 bounded and
|
|
511
|
+
18 660 recovery programs and on 100 000 generated ones. Graph integrity is
|
|
512
|
+
checked after every operation.
|
|
513
|
+
- **Nothing is optional.** Of the 67 ablations in `research/mutants.mjs`, 64
|
|
514
|
+
make a test fail. The other 3 remove store orders kept as conventions
|
|
515
|
+
(truthful transient state); the contract does not require them, and the
|
|
516
|
+
[log](research/LOG.md) records why no test kills them. All 7 ablations of the
|
|
517
|
+
development diagnostic fail a test of the development build.
|
|
518
|
+
- Current Reflex is the same contract plus three failure-path choices. One of
|
|
519
|
+
them drops an execution obligation that the contract (R2) says survives a
|
|
520
|
+
failure.
|
|
521
|
+
|
|
522
|
+
Read more: the [report](research/REPORT.md), the
|
|
523
|
+
[design log](research/LOG.md) (one counterexample per mechanism), and the
|
|
524
|
+
[`padawan`](https://github.com/volynetstyle/reflex-quant/tree/padawan) branch.
|
|
525
|
+
That branch has the same semantics without the engineering (recursive push and pull,
|
|
526
|
+
no counters), written as a guided tour, and it passes the same differential corpus.
|
|
527
|
+
|
|
528
|
+
## Under the hood
|
|
529
|
+
|
|
530
|
+
The runtime has three layers:
|
|
531
|
+
|
|
532
|
+
- **kernel**: the dependency machine (graph, tracking, propagation, validation);
|
|
533
|
+
- **host**: effect lifecycle and delivery;
|
|
534
|
+
- **surface**: the typed public API. It is a thin layer: no operation adds a field,
|
|
535
|
+
a flag or a branch on a hot path ([research/LOG.md](research/LOG.md) item 26).
|
|
536
|
+
|
|
537
|
+
The production build strips research instrumentation and development
|
|
538
|
+
diagnostics. The illustrated
|
|
539
|
+
[anatomy page](https://volynetstyle.github.io/reflex-quant/reactive-runtime-anatomy/)
|
|
540
|
+
walks through each part.
|
|
110
541
|
|
|
111
542
|
```text
|
|
112
|
-
src/ graph representation, kernel
|
|
543
|
+
src/ graph representation, kernel, host, public surface, instrumentation
|
|
113
544
|
test/ differential (spec, contract spec, Reflex, quant), tracking,
|
|
114
545
|
semantics, structural bounds; tools/ (QUANT_TOOLS=1)
|
|
115
546
|
bench/ scenarios, adapters, build, timing (CI only), structure counters
|
|
116
|
-
research/ evidence, design log, ablations,
|
|
547
|
+
research/ evidence, design log, ablations, historical benchmark snapshots
|
|
117
548
|
reference/ frozen Reflex implementation and provenance fixtures
|
|
549
|
+
site/ the website (Astro)
|
|
118
550
|
```
|
|
119
551
|
|
|
120
|
-
|
|
552
|
+
<details>
|
|
553
|
+
<summary><b>Contributing: build, test and benchmark</b></summary>
|
|
121
554
|
|
|
122
|
-
|
|
555
|
+
Development (including the site) uses Node.js 22.23.4 (see `.nvmrc`) or 24+
|
|
556
|
+
and npm. The published library supports Node.js 18+. Run `npm ci` to install.
|
|
557
|
+
The Reflex sources, spec harness and fixed production baseline live in
|
|
558
|
+
`reference/reflex`, so you don't need a Reflex checkout.
|
|
123
559
|
|
|
124
560
|
```sh
|
|
125
561
|
npm run lint # lint production TypeScript
|
|
@@ -141,4 +577,32 @@ node bench/cost/build.mjs && node bench/cost/run.mjs regime --rounds 0
|
|
|
141
577
|
# cost-model probes (research/COST-MODEL.md); timing on CI
|
|
142
578
|
```
|
|
143
579
|
|
|
144
|
-
CI
|
|
580
|
+
CI runs on Windows, macOS and Linux with Node 22.23.4 and 24. Each matrix job
|
|
581
|
+
checks declarations, release artifacts, development and production tests, cost
|
|
582
|
+
probe verification, and the site. Dependencies are installed from both lockfiles
|
|
583
|
+
with `npm ci` (root and `site/`).
|
|
584
|
+
|
|
585
|
+
`npm run check:bench` builds every cost probe and runs it in verification mode,
|
|
586
|
+
including the independent leaf and decomposition cells, without collecting timings.
|
|
587
|
+
`npm run check:bench -- --smoke` limits this to leaf and decomposition.
|
|
588
|
+
`npm run check:site` builds the runtime and bench bundles, compares the site's
|
|
589
|
+
visualizations with the runtime, builds Astro, and checks the generated internal
|
|
590
|
+
page and asset URLs under the deployment base. Install site dependencies first
|
|
591
|
+
with `npm ci --prefix site`.
|
|
592
|
+
|
|
593
|
+
`.github/workflows/benchmark.yml` verifies all cost probes before any timing job
|
|
594
|
+
starts, and it shares the verified bundles. Each timing job runs all three
|
|
595
|
+
runtimes on one runner: compare ratios within a job, never times across jobs.
|
|
596
|
+
Pages deployment uploads only a site that passed its checks. The Reflex
|
|
597
|
+
benchmark baseline is fixed at commit `bf71877`; see
|
|
598
|
+
`reference/reflex/README.md` for its provenance.
|
|
599
|
+
|
|
600
|
+
Before proposing a change to the runtime, read [AGENTS.md](AGENTS.md) and
|
|
601
|
+
[ARCHITECTURE.md](ARCHITECTURE.md). They describe the inclusion criterion
|
|
602
|
+
a new mechanism has to meet.
|
|
603
|
+
|
|
604
|
+
</details>
|
|
605
|
+
|
|
606
|
+
## License
|
|
607
|
+
|
|
608
|
+
[MIT](LICENSE) © Andrii Volynets
|