@hidemikimura/vanji 0.1.0 → 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ ## 0.1.1 (2026-12)
4
+
5
+ No change to the public API.
6
+
7
+ - Faster updates of list rows: a text hole that is an element's only content keeps its text node on the row from the first update on, and later strings are written to it directly. Updating every 10th row of 1,000 went from about 1.4× to 1.2× the time of hand-written JavaScript (each operation repeated 400 times).
8
+ - A binding made only of `selected() === row.id ? 'a' : 'b'` is no longer re-evaluated when the item is replaced; it is written only when the selection changes.
9
+ - Fast lists no longer keep a copy of the previous items array.
10
+ - Cost: one more field per text hole per row (JS heap at 10,000 rows 1,951 KB → 1,996 KB) and 28 bytes more runtime (3,827 bytes gzipped).
11
+
12
+ ## 0.1.0 (2026-12-02)
13
+
14
+ First release.
package/README.md CHANGED
@@ -158,46 +158,56 @@ node memory.mjs # メモリ(強制 GC 後。V8 の JS ヒープ + Blink
158
158
 
159
159
  | 操作 | 素の JS | vanji | lit-html | Solid | Svelte | thane |
160
160
  | --- | --- | --- | --- | --- | --- | --- |
161
- | 1,000 行作成 | 1.53 | 1.50 | 4.25 | 2.54 | 3.27 | 1.51 |
162
- | 1,000 行置換 | 2.85 | 2.92 | 6.07 | 3.87 | 5.08 | 2.88 |
163
- | 10 行おきに更新 | 0.040 | 0.080 | 0.180 | 0.125 | 0.200 | 0.065 |
164
- | 行の選択 | 0.010 | 0.025 | 0.140 | 0.050 | 0.340 | 0.015 |
165
- | 2 行入れ替え | 0.025 | 0.055 | 0.185 | 0.175 | 0.245 | 0.040 |
166
- | 1 行削除 | 0.020 | 0.055 | 0.160 | 0.080 | 0.185 | 0.045 |
167
- | 1,000 行に 1,000 行追加 | 1.49 | 1.54 | 4.29 | 2.68 | 3.52 | 1.50 |
168
- | 1,000 行クリア | 1.41 | 1.43 | 1.97 | 1.59 | 1.76 | 1.46 |
169
- | 10,000 行作成 | 14.2 | 15.8 | 34.3 | 21.1 | 27.8 | 15.4 |
161
+ | 1,000 行作成 | 1.59 | 1.64 | 4.26 | 2.66 | 3.26 | 1.63 |
162
+ | 1,000 行置換 | 2.94 | 3.13 | 6.36 | 4.04 | 4.67 | 3.01 |
163
+ | 10 行おきに更新 | 0.045 | 0.060 | 0.180 | 0.130 | 0.225 | 0.065 |
164
+ | 行の選択 | 0.010 | 0.020 | 0.140 | 0.045 | 0.355 | 0.015 |
165
+ | 2 行入れ替え | 0.020 | 0.050 | 0.175 | 0.165 | 0.255 | 0.045 |
166
+ | 1 行削除 | 0.020 | 0.055 | 0.165 | 0.085 | 0.195 | 0.045 |
167
+ | 1,000 行に 1,000 行追加 | 1.91 | 1.99 | 4.36 | 2.72 | 3.42 | 1.72 |
168
+ | 1,000 行クリア | 1.48 | 1.83 | 2.16 | 1.69 | 2.12 | 1.60 |
169
+ | 10,000 行作成 | 15.1 | 16.4 | 37.0 | 22.1 | 29.0 | 15.9 |
170
170
 
171
171
  合計時間(スクリプト + スタイル計算 + レイアウト、ms):
172
172
 
173
173
  | 操作 | 素の JS | vanji | lit-html | Solid | Svelte | thane |
174
174
  | --- | --- | --- | --- | --- | --- | --- |
175
- | 1,000 行作成 | 15.1 | 15.3 | 17.9 | 16.1 | 17.5 | 15.0 |
176
- | 1,000 行置換 | 12.5 | 12.5 | 15.8 | 13.4 | 14.5 | 12.7 |
177
- | 10 行おきに更新 | 3.29 | 3.40 | 3.49 | 3.40 | 3.50 | 3.25 |
178
- | 行の選択 | 0.055 | 0.065 | 0.245 | 0.085 | 0.385 | 0.055 |
179
- | 2 行入れ替え | 1.14 | 1.12 | 1.36 | 1.28 | 1.34 | 1.19 |
180
- | 1 行削除 | 1.06 | 1.08 | 1.22 | 1.08 | 1.23 | 1.04 |
181
- | 1,000 行に 1,000 行追加 | 12.4 | 12.5 | 15.3 | 13.3 | 14.0 | 12.3 |
182
- | 1,000 行クリア | 1.47 | 1.50 | 2.12 | 1.65 | 1.83 | 1.53 |
183
- | 10,000 行作成 | 112.5 | 113.6 | 137.7 | 118.0 | 126.5 | 111.3 |
175
+ | 1,000 行作成 | 15.9 | 15.7 | 18.6 | 16.6 | 17.2 | 15.6 |
176
+ | 1,000 行置換 | 13.1 | 13.6 | 16.9 | 14.3 | 14.8 | 13.1 |
177
+ | 10 行おきに更新 | 3.31 | 3.37 | 3.42 | 3.42 | 3.46 | 3.23 |
178
+ | 行の選択 | 0.050 | 0.055 | 0.255 | 0.090 | 0.400 | 0.060 |
179
+ | 2 行入れ替え | 1.12 | 1.17 | 1.37 | 1.25 | 1.33 | 1.14 |
180
+ | 1 行削除 | 1.07 | 1.10 | 1.29 | 1.11 | 1.19 | 1.09 |
181
+ | 1,000 行に 1,000 行追加 | 16.9 | 16.7 | 15.8 | 13.9 | 14.7 | 14.9 |
182
+ | 1,000 行クリア | 1.54 | 1.89 | 2.33 | 1.75 | 2.20 | 1.68 |
183
+ | 10,000 行作成 | 127.5 | 122.2 | 154.9 | 134.2 | 141.8 | 120.8 |
184
+
185
+ 軽い操作を繰り返した時間(µs、括弧内は素の JS との比)。上の表の 0.1ms 未満の値はタイマーの 5µs 刻みに対して数刻みしかないので、`micro.mjs` で 1 ページの中で 400 回繰り返し(20 回ごとに表を作り直し、レイアウトは測らない)、1 回あたりの時間を 7 ページで比べた中央値です。軽い操作の比はこちらで読んでください:
186
+
187
+ | 操作 | 素の JS | vanji | lit-html | Solid | Svelte | thane |
188
+ | --- | --- | --- | --- | --- | --- | --- |
189
+ | 10 行おきに更新 | 54.9 | 67.9 (1.24×) | 157.4 (2.87×) | 108.2 (1.97×) | 130.0 (2.37×) | 62.6 (1.14×) |
190
+ | 行の選択 | 1.1 | 1.9 (1.73×) | 78.6 (71×) | 8.1 (7.36×) | 145.9 (133×) | 2.6 (2.36×) |
191
+ | 2 行入れ替え | 19.2 | 25.0 (1.30×) | 101.6 (5.29×) | 106.0 (5.52×) | 87.0 (4.53×) | 24.6 (1.28×) |
192
+ | 1 行削除 | 18.8 | 31.3 (1.66×) | 122.2 (6.50×) | 58.4 (3.11×) | 72.3 (3.85×) | 29.1 (1.55×) |
184
193
 
185
194
  メモリ(描画・レイアウト後に強制 GC してからの増分。環境は上と同じ。`memory.mjs` を 3 回実行した中央値)。
186
195
  「合計」は V8 の JS ヒープと Blink のヒープ(DOM ノード・イベントリスナーなど。CDP の `Runtime.getHeapUsage` の embedderHeapUsedSize)の和です。1,000 行の Blink ヒープと、10,000 行の vanji・thane・Solid は、3 回のうち 1 回が ±350〜600KB ずれることがあります(GC のタイミングと推測、未特定):
187
196
 
188
197
  | 指標 | 素の JS | vanji | lit-html | Solid | Svelte | thane |
189
198
  | --- | --- | --- | --- | --- | --- | --- |
190
- | 10,000 行: 合計(JS + Blink) | 72,458KB | 72,709KB | 89,406KB | 81,625KB | 84,107KB | 72,559KB |
191
- | 10,000 行: うち JS ヒープ | 1,703KB | 1,951KB | 7,089KB | 9,620KB | 13,201KB | 1,788KB |
192
- | 10,000 行: うち Blink ヒープ | 70,755KB | 70,757KB | 82,317KB | 72,005KB | 70,906KB | 70,772KB |
193
- | 1,000 行: 合計(JS + Blink) | 7,606KB | 7,678KB | 9,081KB | 8,540KB | 8,891KB | 7,642KB |
194
- | 1,000 行: うち JS ヒープ | 221KB | 293KB | 744KB | 1,057KB | 1,516KB | 243KB |
195
- | 参考: `measureUserAgentSpecificMemory`(10,000 行) | 10,913KB | 11,168KB | 23,511KB | 19,008KB | 22,586KB | 11,017KB |
199
+ | 10,000 行: 合計(JS + Blink) | 72,458KB | 72,757KB | 89,406KB | 81,625KB | 84,107KB | 72,559KB |
200
+ | 10,000 行: うち JS ヒープ | 1,703KB | 1,996KB | 7,089KB | 9,620KB | 13,201KB | 1,788KB |
201
+ | 10,000 行: うち Blink ヒープ | 70,755KB | 70,761KB | 82,317KB | 72,005KB | 70,906KB | 70,772KB |
202
+ | 1,000 行: 合計(JS + Blink) | 7,606KB | 7,681KB | 9,081KB | 8,540KB | 8,891KB | 7,642KB |
203
+ | 1,000 行: うち JS ヒープ | 221KB | 296KB | 744KB | 1,057KB | 1,516KB | 243KB |
204
+ | 参考: `measureUserAgentSpecificMemory`(10,000 行) | 10,913KB | 11,223KB | 23,511KB | 19,008KB | 22,586KB | 11,017KB |
196
205
 
197
206
  読み方の注意:
198
207
 
199
208
  - **絶対値は 3 回の実行で 10〜28% ぶれます**(重い操作)。同じ回・同じ操作の中では実装がそろって動くので、実装どうしは素の JS との比で比べてください。ぶれの原因は未特定です(Headless Shell 版では 5% 以内でした)。
200
- - 0.1ms 未満の値(更新・選択・入れ替え・1 行削除のスクリプト時間)は、タイマーの 5µs 刻みに対して数刻みしかなく、比は粗くなります。
209
+ - 0.1ms 未満の値(更新・選択・入れ替え・1 行削除のスクリプト時間)は、タイマーの 5µs 刻みに対して数刻みしかなく、比は粗くなります(例: 1 行削除は表では 2.60〜3.33 倍ですが、繰り返して測ると 1.66 倍)。比べるときは「軽い操作を繰り返した時間」の表を使ってください。
210
+ - この回は 1,000 行追加の素の JS(1.91ms)と 1,000 行クリアの vanji(1.83ms)が前回(1.53ms・1.47ms)より遅く出ました。どちらの処理も変えていないので、ぶれと考えています(原因は未特定)。
201
211
  - **vanji v2(2026-12、thane を参考にした作り直し)の前後**(スクリプト時間の素の JS 比、各 3 回):
202
212
 
203
213
  | 操作 | v1 | v2 | thane |
@@ -213,13 +223,14 @@ node memory.mjs # メモリ(強制 GC 後。V8 の JS ヒープ + Blink
213
223
  | 10,000 行作成 | 1.43〜1.50 | 1.05〜1.15 | 1.03〜1.14 |
214
224
 
215
225
  重い操作は素の JS・thane とほぼ同じになりました。10 行おきの更新は v1 より遅いままです(0.075 → 0.080ms、thane 0.065ms)。1 行削除は差分処理の先頭・末尾の比較を軽くして v1 と thane の間まで戻りました(v1 0.040ms、v2 0.055ms、thane 0.045ms)。どちらも v2 のベンチマーク実装がイミュータブル更新(配列を作り直し、キーで比べる)になったことが主な理由です。JS ヒープは 10,000 行で 6,840KB → 1,951KB です。
226
+ - **10 行おきの更新の改善**(2026-12、0.1.1): 要素の唯一の子であるテキストの穴は、最初の更新でテキストノードを行に持ち、次からはそこへ直接書くようにしました(要素パスをたどらない)。あわせて、選択だけのバインディング(`selected() === row.id ? 'danger' : ''`)はアイテムの入れ替えでは読み直さず、前回のアイテムの配列も持たないようにしました(この 2 つは単独ではぶれの範囲でした)。操作を 400 回繰り返して測ると、更新 1 回は素の JS 比 1.36〜1.41 倍 → 1.14〜1.22 倍(thane 1.14〜1.21 倍)、上の表では 1.89〜2.00 倍 → 1.33〜1.63 倍です。代わりに 10,000 行の JS ヒープが 1,951KB → 1,996KB(行ごとに 1 フィールド × 穴の数)、ランタイムが gzip で +28B(全体 3,827B)増えました。1 行削除は表では 2.75〜3.33 → 3.33〜3.67 倍ですが、400 回繰り返した計測では変更前と差がなく(26.8µs → 28.1µs、各ページの値は重なる)、素の JS の値が 5µs 刻みで 0.020 → 0.015ms になったことが主な理由です。
216
227
  - レイアウト時間はフレームワークの差ではありません。トレースで確認すると、4 実装ともブラウザが再計算する対象は同数です(1 行削除: 9,996 個中 6 個、1,000 行作成: 10,006 個)。
217
228
  - 以前の 1 コア環境(Chromium 153)では、合計時間の「1,000 行作成」で lit-html が最速に見え、lit-html の置換・クリアが 100ms を超えていました。複数コアのこの環境ではどちらも再現しません。
218
229
  - 素の JS・Solid・Svelte・thane・vanji はクリックイベントを委譲します(素の JS・thane・vanji は `<tbody>` に 1 つ、Solid・Svelte は `document`)。lit-html だけ行ごとに `addEventListener` しています。
219
230
  - vanji と thane は行をただのオブジェクトにしてイミュータブルに更新し、Solid は行ごとにラベルのシグナルを持たせています(それぞれのフレームワークの定石)。
220
231
  - メモリの大半(約 9 割)は Blink のヒープで、DOM ノードの数でほぼ決まります。`measureUserAgentSpecificMemory` は Blink のヒープを含まないので参考値です。
221
232
  - ブラウザは `bench/browser.mjs` で起動し、全スクリプトで Playwright のフル版 Chromium を使います。別のバイナリは `CHROME_PATH` で指定できます。
222
- - 調査用スクリプト: `profile.mjs`(操作単位の CPU プロファイル。`npx vite build --minify false --outDir dist-prof` が必要)、`trace.mjs`(レイアウト対象数)、`trace-gc.mjs`(経過時間と CPU 時間の比較)、`trace-threads.mjs`(他スレッドの稼働)、`layout-lab.mjs`(フレームワークなしの DOM 実験)。
233
+ - 調査用スクリプト: `micro.mjs`(軽い操作を 1 ページで数百回繰り返した 1 回あたりの時間。`OP=update|remove`)、`profile-rep.mjs`(軽い操作を繰り返した CPU プロファイル。関数ごとの 1 回あたりの時間)、`profile.mjs`(操作単位の CPU プロファイル。どちらも `npx vite build --minify false --outDir dist-prof` が必要)、`trace.mjs`(レイアウト対象数)、`trace-gc.mjs`(経過時間と CPU 時間の比較)、`trace-threads.mjs`(他スレッドの稼働)、`layout-lab.mjs`(フレームワークなしの DOM 実験)。
223
234
 
224
235
  ## 構成
225
236
 
package/compiler/list.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Fast lists: `${each(items, row => html`<one element>…`, { key })}` compiled into a row record
2
2
  // factory for src/list.js. Returns null when the row cannot be compiled this way; the generic each()
3
3
  // then handles it (same behaviour, a scope per row).
4
- import { hasFnOrTemplate, IDENT } from './ast.js';
4
+ import { hasFnOrTemplate, walk, IDENT } from './ast.js';
5
5
 
6
6
  const prop = p => (IDENT.test(p) ? `.${p}` : `[${JSON.stringify(p)}]`);
7
7
 
@@ -56,7 +56,20 @@ export function emitList(call, parent, marker, api, err) {
56
56
  const params = `(${rowParam}${idxParam ? `, ${idxParam}` : ''})`;
57
57
  const bindRow = `const ${rowParam} = _$$v${idxParam ? `, ${idxParam} = _$$i` : ''};`;
58
58
 
59
- const hoisted = [], fields = [], initial = [], patches = [], dyn = [], sigs = new Set(), events = {};
59
+ const hoisted = [], fields = [], initial = [], patches = [], selPatches = [], dyn = [], sigs = new Set(), events = {};
60
+ // do the branches of a selection (`sel() === row.key ? a : b`) mention the row or the index?
61
+ // (conservative: any identifier of that name; the test itself reads only the key)
62
+ const names = new Set([rowName, row.params[1] && row.params[1].name].filter(Boolean));
63
+ const readsRow = e => {
64
+ if (e.type !== 'ConditionalExpression') return false;
65
+ let found = false;
66
+ for (const branch of [e.consequent, e.alternate])
67
+ walk(branch, n => {
68
+ if (found) return false;
69
+ if (n.type === 'Identifier' && names.has(n.name)) found = true;
70
+ });
71
+ return found;
72
+ };
60
73
  let uid = 0;
61
74
  const fn = body => {
62
75
  const name = `_$$f${uid++}`;
@@ -89,13 +102,26 @@ export function emitList(call, parent, marker, api, err) {
89
102
  }
90
103
  // the value of this binding as an expression of the row, and how it behaves
91
104
  const holes = b.type === 'text' ? [b.index] : b.a.parts.filter(p => typeof p !== 'string').map(p => p.hole);
92
- let mode = 'memo';
105
+ let mode = 'memo', selects = false, onlySelect = true;
93
106
  for (const i of holes) {
94
107
  const e = exprs[i];
95
108
  const s = selectorOf(e, rowName, keyProp, isHtmlTag, calls);
96
- if (s) sigs.add(s);
97
- else if (calls(e)) mode = 'dyn';
109
+ if (s) {
110
+ sigs.add(s);
111
+ selects = true;
112
+ if (readsRow(e)) onlySelect = false;
113
+ } else {
114
+ onlySelect = false;
115
+ if (calls(e)) mode = 'dyn';
116
+ }
98
117
  }
118
+ // where the patch goes: u (the item or index changed) and/or s (a selection signal changed).
119
+ // A binding made only of `sig() === row.key ? a : b` with branches that do not read the row
120
+ // cannot change with the item (same key), so it is left out of u.
121
+ const patchTo = code => {
122
+ if (selects) selPatches.push(code);
123
+ if (!(selects && onlySelect)) patches.push(code);
124
+ };
99
125
  let value;
100
126
  if (b.type === 'attr' && !(b.a.parts.length === 1 && typeof b.a.parts[0] !== 'string'))
101
127
  value = b.a.parts.map(p => (typeof p === 'string' ? JSON.stringify(p) : `${use('str')}(${source(exprs[p.hole])})`)).join(' + ');
@@ -120,14 +146,19 @@ export function emitList(call, parent, marker, api, err) {
120
146
  const p = `p${uid++}`;
121
147
  fields.push(`${p}: (${value})`);
122
148
  if (b.type === 'text' && b.marker === null) {
123
- const at = keep(b.parent, b.path, b.safe);
149
+ // from its first update on, the row keeps the text node and writes new strings to it directly
150
+ const at = keep(b.parent, b.path, b.safe), x = `x${uid++}`;
151
+ fields.push(`${x}: null`);
124
152
  initial.push(`${use('text')}(${b.parent}, _$$r.${p});`);
125
- patches.push(`if (_$$r.${p} !== (_$$t = (${value}))) { _$$r.${p} = _$$t; ${use('text')}(${at}, _$$t); }`);
153
+ patchTo(
154
+ `if (_$$r.${p} !== (_$$t = (${value}))) { _$$r.${p} = _$$t; ` +
155
+ `if (_$$r.${x} !== null && typeof _$$t === 'string') _$$r.${x}.data = _$$t; else _$$r.${x} = ${use('textNode')}(${at}, _$$t); }`
156
+ );
126
157
  } else if (b.type === 'text') {
127
158
  const n = `n${uid++}`, m = `m${uid++}`;
128
159
  fields.push(`${n}: undefined`, `${m}: ${b.marker}`);
129
160
  initial.push(`_$$r.${n} = ${use('place')}(${b.parent}, _$$r.${p}, undefined, ${b.marker});`);
130
- patches.push(`if (_$$r.${p} !== (_$$t = (${value}))) { _$$r.${p} = _$$t; _$$r.${n} = ${use('place')}(_$$r.${m}.parentNode, _$$t, _$$r.${n}, _$$r.${m}); }`);
161
+ patchTo(`if (_$$r.${p} !== (_$$t = (${value}))) { _$$r.${p} = _$$t; _$$r.${n} = ${use('place')}(_$$r.${m}.parentNode, _$$t, _$$r.${n}, _$$r.${m}); }`);
131
162
  } else {
132
163
  const at = keep(b.el, b.path, b.safe), name = b.a.name;
133
164
  let now, later;
@@ -146,7 +177,7 @@ export function emitList(call, parent, marker, api, err) {
146
177
  later = `${use('setAttr')}(${at}, ${JSON.stringify(name)}, _$$t);`;
147
178
  }
148
179
  initial.push(now);
149
- patches.push(`if (_$$r.${p} !== (_$$t = (${value}))) { _$$r.${p} = _$$t; ${later} }`);
180
+ patchTo(`if (_$$r.${p} !== (_$$t = (${value}))) { _$$r.${p} = _$$t; ${later} }`);
150
181
  }
151
182
  }
152
183
 
@@ -156,8 +187,9 @@ export function emitList(call, parent, marker, api, err) {
156
187
  `${initial.join(' ')} ${dyn.length ? `${use('rowScope')}(_$$r, () => { ${dyn.join(' ')} });` : ''} return _$$r; }`;
157
188
  const patch = patches.length ? `(_$$r, _$$v, _$$i) => { ${bindRow} let _$$t; ${patches.join(' ')} }` : '() => {}';
158
189
  const x = sigs.size ? `[${[...sigs].map(s => `() => ${s}()`).join(', ')}]` : 'null';
190
+ const sel = selPatches.length ? `(_$$r) => { const ${rowParam} = _$$r.v${idxParam ? `, ${idxParam} = _$$r.i` : ''}; let _$$t; ${selPatches.join(' ')} }` : 'null';
159
191
  const ev = Object.keys(events).length ? `{ ${Object.entries(events).map(([k, v]) => `${JSON.stringify(k)}: [${v.join(', ')}]`).join(', ')} }` : 'null';
160
- const spec = `{ t: ${register(t)}, k: ${key}, c: ${create}, u: ${patch}, x: ${x}, e: ${ev}, ix: ${!!idxParam} }`;
192
+ const spec = `{ t: ${register(t)}, k: ${key}, c: ${create}, u: ${patch}, s: ${sel}, x: ${x}, e: ${ev}, ix: ${!!idxParam} }`;
161
193
  const specCode = hoisted.length ? `(() => { ${hoisted.join(' ')} return ${spec}; })()` : spec;
162
194
  return `${use('list')}(${parent}, ${marker || 'null'}, () => (${source(items)}), ${specCode});`;
163
195
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hidemikimura/vanji",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A tiny compile-time UI framework: html`` templates + signals, close to vanilla JS in speed and memory",
5
5
  "keywords": [
6
6
  "ui",
@@ -42,6 +42,7 @@
42
42
  "skills",
43
43
  "llms.txt",
44
44
  "README.md",
45
+ "CHANGELOG.md",
45
46
  "LICENSE"
46
47
  ],
47
48
  "publishConfig": {
package/src/internal.js CHANGED
@@ -1,4 +1,4 @@
1
1
  // Helpers imported by compiled templates. Not part of the public API.
2
2
  export { template, insert, attr, setAttr, bool, str, c, each, when, place, memo, nodes } from './dom.js';
3
- export { list, text, rowScope } from './list.js';
3
+ export { list, text, textNode, rowScope } from './list.js';
4
4
  export { render as effect } from './signals.js';
package/src/list.js CHANGED
@@ -25,6 +25,17 @@ export const text = (el, v) => {
25
25
  }
26
26
  };
27
27
 
28
+ /**
29
+ * text(), then the text node when the value is a string or number (else null). Rows keep it from
30
+ * their first update on and write later strings to it directly; it is not taken when the row is
31
+ * created, because a kept node costs a JS wrapper per row.
32
+ */
33
+ export const textNode = (el, v) => {
34
+ text(el, v);
35
+ const t = typeof v;
36
+ return t === 'string' || t === 'number' ? el.firstChild : null;
37
+ };
38
+
28
39
  const rerunAll = s => {
29
40
  const o = s.o;
30
41
  if (o === null) return;
@@ -48,13 +59,13 @@ const call = (h, node, e) => {
48
59
  * @param {Node | null} marker rows go before it (null: the rows are the parent's only content)
49
60
  * @param {() => any[]} items
50
61
  * @param {{ t: () => Element, k?: string | ((item: any) => any), c: Function, u: Function,
51
- * x?: Array<() => any> | null, e?: Record<string, Array<[Function, Function]>> | null, ix?: boolean }} spec
62
+ * s?: Function | null, x?: Array<() => any> | null, e?: Record<string, Array<[Function, Function]>> | null, ix?: boolean }} spec
52
63
  */
53
64
  export const list = (parent, marker, items, spec) => {
54
65
  const make = spec.t, create = spec.c, patch = spec.u, ix = !!spec.ix, k = spec.k;
55
66
  const keyOf = k == null ? v => v : typeof k === 'function' ? k : v => v[k];
56
67
  const map = new Map(); // key -> row
57
- let recs = [], prev = [], stamp = 0;
68
+ let recs = [], stamp = 0; // recs[i].v is the item the row shows (no copy of the previous array)
58
69
 
59
70
  const fresh = (item, i) => create(make(), item, i);
60
71
  const refresh = (r, item, i) => {
@@ -75,7 +86,6 @@ export const list = (parent, marker, items, spec) => {
75
86
  for (let i = 0; i < recs.length; i++) if (recs[i].s !== null) dispose(recs[i].s);
76
87
  map.clear();
77
88
  recs = [];
78
- prev = [];
79
89
  };
80
90
 
81
91
  const update = items => {
@@ -93,28 +103,24 @@ export const list = (parent, marker, items, spec) => {
93
103
  parent.insertBefore(r.el, marker);
94
104
  }
95
105
  if (map.size !== n) duplicate(items);
96
- prev = items.slice();
97
106
  return;
98
107
  }
99
108
  // 1. unchanged (same key) head and tail; replaced items are patched in place
100
109
  // (identity is checked inline: most items are unchanged, so the key is rarely needed)
101
110
  let ns = 0, os = 0, ne = n, oe = pn, a, b;
102
111
  while (ns < ne && os < oe) {
103
- if ((a = items[ns]) !== (b = prev[os])) {
112
+ if ((a = items[ns]) !== (b = recs[os].v)) {
104
113
  if (keyOf(a) !== keyOf(b)) break;
105
114
  refresh(recs[os], a, ns);
106
115
  }
107
116
  ns++, os++;
108
117
  }
109
118
  while (ns < ne && os < oe) {
110
- if ((a = items[ne - 1]) !== (b = prev[oe - 1]) && keyOf(a) !== keyOf(b)) break;
119
+ if ((a = items[ne - 1]) !== (b = recs[oe - 1].v) && keyOf(a) !== keyOf(b)) break;
111
120
  ne--, oe--;
112
121
  if (a !== b || (ix && ne !== oe)) refresh(recs[oe], a, ne);
113
122
  }
114
- if (ns === ne && os === oe) {
115
- prev = items.slice();
116
- return;
117
- }
123
+ if (ns === ne && os === oe) return;
118
124
  const mid = new Array(ne - ns);
119
125
  const after = oe < pn ? recs[oe].el : marker;
120
126
  if (os === oe) {
@@ -127,22 +133,22 @@ export const list = (parent, marker, items, spec) => {
127
133
  } else if (ns === ne) {
128
134
  // only removals
129
135
  for (i = os; i < oe; i++) {
130
- map.delete(keyOf(prev[i]));
136
+ map.delete(keyOf(recs[i].v));
131
137
  drop(recs[i]);
132
138
  }
133
139
  } else {
134
140
  // 2. rows that swapped ends need no lookup
135
141
  let s = ns, e = ne, ps = os, pe = oe;
136
142
  while (s < e && ps < pe) {
137
- if (same(items[s], prev[ps])) {
143
+ if (same(items[s], recs[ps].v)) {
138
144
  r = mid[s - ns] = recs[ps];
139
145
  if (r.v !== items[s] || (ix && s !== ps)) refresh(r, items[s], s);
140
146
  s++, ps++;
141
- } else if (same(items[e - 1], prev[pe - 1])) {
147
+ } else if (same(items[e - 1], recs[pe - 1].v)) {
142
148
  r = mid[e - 1 - ns] = recs[pe - 1];
143
149
  if (r.v !== items[e - 1] || (ix && e - 1 !== pe - 1)) refresh(r, items[e - 1], e - 1);
144
150
  e--, pe--;
145
- } else if (same(items[s], prev[pe - 1]) && same(items[e - 1], prev[ps])) {
151
+ } else if (same(items[s], recs[pe - 1].v) && same(items[e - 1], recs[ps].v)) {
146
152
  const a = recs[ps], b = recs[pe - 1], next = b.el.nextSibling;
147
153
  parent.insertBefore(b.el, a.el);
148
154
  parent.insertBefore(a.el, next);
@@ -178,7 +184,7 @@ export const list = (parent, marker, items, spec) => {
178
184
  } else {
179
185
  for (i = ps; i < pe; i++)
180
186
  if (recs[i].h !== stamp) {
181
- map.delete(keyOf(prev[i]));
187
+ map.delete(keyOf(recs[i].v));
182
188
  drop(recs[i]);
183
189
  }
184
190
  let ref = e < ne ? mid[e - ns].el : after;
@@ -193,7 +199,6 @@ export const list = (parent, marker, items, spec) => {
193
199
  }
194
200
  recs = recs.slice(0, ns).concat(mid, recs.slice(oe));
195
201
  if (map.size !== n) duplicate(items);
196
- prev = items.slice();
197
202
  };
198
203
 
199
204
  const duplicate = items => {
@@ -215,6 +220,9 @@ export const list = (parent, marker, items, spec) => {
215
220
  });
216
221
 
217
222
  // `sig() === row.key` holes: one subscription for the list, only the old and new rows are patched
223
+ // spec.s patches only the bindings that read a selection signal (they are left out of spec.u
224
+ // when the item cannot change them)
225
+ const select = spec.s || (r => patch(r, r.v, r.i));
218
226
  if (spec.x)
219
227
  for (const sig of spec.x) {
220
228
  let cur = untrack(sig);
@@ -223,8 +231,8 @@ export const list = (parent, marker, items, spec) => {
223
231
  if (v === cur) return;
224
232
  const a = map.get(cur), b = map.get(v);
225
233
  cur = v;
226
- if (a !== undefined) untrack(() => patch(a, a.v, a.i));
227
- if (b !== undefined) untrack(() => patch(b, b.v, b.i));
234
+ if (a !== undefined) untrack(select, a);
235
+ if (b !== undefined) untrack(select, b);
228
236
  });
229
237
  }
230
238