Skip to content

Commit fb0661c

Browse files
committed
merge main
2 parents c8f4868 + 808cc6f commit fb0661c

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

.changeset/three-lions-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: more efficient each block compiler output

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,12 +2326,6 @@ export const template_visitors = {
23262326
// TODO should use context.visit?
23272327
const children = create_block(node, 'each_block', node.body.nodes, context);
23282328

2329-
const else_block = node.fallback
2330-
? b.arrow(
2331-
[b.id('$$anchor')],
2332-
/** @type {import('estree').BlockStatement} */ (context.visit(node.fallback))
2333-
)
2334-
: b.literal(null);
23352329
const key_function = node.key
23362330
? b.arrow(
23372331
[node.context.type === 'Identifier' ? node.context : b.id('$$item'), index],
@@ -2349,40 +2343,46 @@ export const template_visitors = {
23492343
declarations.push(b.let(node.index, index));
23502344
}
23512345

2346+
let callee = '$.each_indexed';
2347+
2348+
/** @type {import('estree').Expression[]} */
2349+
const args = [];
2350+
23522351
if ((each_type & EACH_KEYED) !== 0) {
23532352
if (context.state.options.dev && key_function.type !== 'Literal') {
23542353
context.state.init.push(
23552354
b.stmt(b.call('$.validate_each_keys', b.thunk(collection), key_function))
23562355
);
23572356
}
23582357

2359-
context.state.init.push(
2360-
b.stmt(
2361-
b.call(
2362-
'$.each_keyed',
2363-
context.state.node,
2364-
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2365-
b.literal(each_type),
2366-
key_function,
2367-
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
2368-
else_block
2369-
)
2370-
)
2358+
callee = '$.each_keyed';
2359+
2360+
args.push(
2361+
context.state.node,
2362+
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2363+
b.literal(each_type),
2364+
key_function,
2365+
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children)))
23712366
);
23722367
} else {
2373-
context.state.init.push(
2374-
b.stmt(
2375-
b.call(
2376-
'$.each_indexed',
2377-
context.state.node,
2378-
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2379-
b.literal(each_type),
2380-
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
2381-
else_block
2382-
)
2368+
args.push(
2369+
context.state.node,
2370+
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2371+
b.literal(each_type),
2372+
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children)))
2373+
);
2374+
}
2375+
2376+
if (node.fallback) {
2377+
args.push(
2378+
b.arrow(
2379+
[b.id('$$anchor')],
2380+
/** @type {import('estree').BlockStatement} */ (context.visit(node.fallback))
23832381
)
23842382
);
23852383
}
2384+
2385+
context.state.init.push(b.stmt(b.call(callee, ...args)));
23862386
},
23872387
IfBlock(node, context) {
23882388
context.state.template.push('<!>');

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ function each(anchor, get_collection, flags, get_key, render_fn, fallback_fn, re
197197
* @param {number} flags
198198
* @param {null | ((item: V) => string)} get_key
199199
* @param {(anchor: null, item: V, index: import('#client').MaybeSource<number>) => void} render_fn
200-
* @param {null | ((anchor: Node | null) => void)} fallback_fn
200+
* @param {null | ((anchor: Node | null) => void)} [fallback_fn]
201201
* @returns {void}
202202
*/
203-
export function each_keyed(anchor, get_collection, flags, get_key, render_fn, fallback_fn) {
203+
export function each_keyed(anchor, get_collection, flags, get_key, render_fn, fallback_fn = null) {
204204
each(anchor, get_collection, flags, get_key, render_fn, fallback_fn, reconcile_tracked_array);
205205
}
206206

@@ -210,10 +210,10 @@ export function each_keyed(anchor, get_collection, flags, get_key, render_fn, fa
210210
* @param {() => V[]} get_collection
211211
* @param {number} flags
212212
* @param {(anchor: null, item: V, index: import('#client').MaybeSource<number>) => void} render_fn
213-
* @param {null | ((anchor: Node | null) => void)} fallback_fn
213+
* @param {null | ((anchor: Node | null) => void)} [fallback_fn]
214214
* @returns {void}
215215
*/
216-
export function each_indexed(anchor, get_collection, flags, render_fn, fallback_fn) {
216+
export function each_indexed(anchor, get_collection, flags, render_fn, fallback_fn = null) {
217217
each(anchor, get_collection, flags, null, render_fn, fallback_fn, reconcile_indexed_array);
218218
}
219219

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,12 @@ export default function Each_string_template($$anchor, $$props) {
1010
var fragment = $.comment($$anchor);
1111
var node = $.first_child(fragment);
1212

13-
$.each_indexed(
14-
node,
15-
() => ['foo', 'bar', 'baz'],
16-
1,
17-
($$anchor, thing, $$index) => {
18-
var text = $.space_frag($$anchor);
13+
$.each_indexed(node, () => ['foo', 'bar', 'baz'], 1, ($$anchor, thing, $$index) => {
14+
var text = $.space_frag($$anchor);
1915

20-
$.render_effect(() => $.set_text(text, `${$.stringify($.unwrap(thing))}, `));
21-
return $.close($$anchor, text);
22-
},
23-
null
24-
);
16+
$.render_effect(() => $.set_text(text, `${$.stringify($.unwrap(thing))}, `));
17+
return $.close($$anchor, text);
18+
});
2519

2620
$.close_frag($$anchor, fragment);
2721
$.pop();

0 commit comments

Comments
 (0)