From 4c0d35b24227559a5f79d608c638dbef84e4c650 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Sat, 29 Mar 2025 15:20:07 +0000 Subject: [PATCH] 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. --- src/parser/converts/block.ts | 44 ++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/parser/converts/block.ts b/src/parser/converts/block.ts index 1d9709da..fc9f3f92 100644 --- a/src/parser/converts/block.ts +++ b/src/parser/converts/block.ts @@ -147,17 +147,17 @@ export function convertIfBlock( }); const consequent = getConsequentFromIfBlock(node); - ifBlock.children.push( - ...convertChildren( - { - nodes: - // Adjust for Svelte v5 - trimChildren(getChildren(consequent)), - }, - ifBlock, - ctx, - ), - ); + for (const child of convertChildren( + { + nodes: + // Adjust for Svelte v5 + trimChildren(getChildren(consequent)), + }, + ifBlock, + ctx, + )) { + ifBlock.children.push(child); + } ctx.scriptLet.closeScope(); if (elseif) { @@ -218,17 +218,17 @@ export function convertIfBlock( ifBlock.else = elseBlock; ctx.scriptLet.nestBlock(elseBlock); - elseBlock.children.push( - ...convertChildren( - { - nodes: - // Adjust for Svelte v5 - trimChildren(elseChildren), - }, - elseBlock, - ctx, - ), - ); + for (const child of convertChildren( + { + nodes: + // Adjust for Svelte v5 + trimChildren(elseChildren), + }, + elseBlock, + ctx, + )) { + elseBlock.children.push(child); + } ctx.scriptLet.closeScope(); extractMustacheBlockTokens(elseBlock, ctx, { startOnly: true });