Skip to content

Commit 4c0d35b

Browse files
committed
perf: iterate children using a for loop
Spread is considerably slower here than iterating through the converted children and pushing them individually. Additionally, spreading the args can result in a large number of params which can cause other problems (sometimes `push` will throw). I suspect the for loop is faster because `...convertChildren(...)` has to iterate the full set in order to allocate it to an array in memory, which it then has to iterate to spread as params. Whereas iterating through one at a time means we can add the child to the block without waiting for the entire iterable to finish.
1 parent 73479f9 commit 4c0d35b

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/parser/converts/block.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,17 @@ export function convertIfBlock(
147147
});
148148
const consequent = getConsequentFromIfBlock(node);
149149

150-
ifBlock.children.push(
151-
...convertChildren(
152-
{
153-
nodes:
154-
// Adjust for Svelte v5
155-
trimChildren(getChildren(consequent)),
156-
},
157-
ifBlock,
158-
ctx,
159-
),
160-
);
150+
for (const child of convertChildren(
151+
{
152+
nodes:
153+
// Adjust for Svelte v5
154+
trimChildren(getChildren(consequent)),
155+
},
156+
ifBlock,
157+
ctx,
158+
)) {
159+
ifBlock.children.push(child);
160+
}
161161

162162
ctx.scriptLet.closeScope();
163163
if (elseif) {
@@ -218,17 +218,17 @@ export function convertIfBlock(
218218
ifBlock.else = elseBlock;
219219

220220
ctx.scriptLet.nestBlock(elseBlock);
221-
elseBlock.children.push(
222-
...convertChildren(
223-
{
224-
nodes:
225-
// Adjust for Svelte v5
226-
trimChildren(elseChildren),
227-
},
228-
elseBlock,
229-
ctx,
230-
),
231-
);
221+
for (const child of convertChildren(
222+
{
223+
nodes:
224+
// Adjust for Svelte v5
225+
trimChildren(elseChildren),
226+
},
227+
elseBlock,
228+
ctx,
229+
)) {
230+
elseBlock.children.push(child);
231+
}
232232
ctx.scriptLet.closeScope();
233233
extractMustacheBlockTokens(elseBlock, ctx, { startOnly: true });
234234

0 commit comments

Comments
 (0)