Skip to content

Commit 7eabce8

Browse files
authored
chore: improve the performance of set_text for single expressions (#12893)
1 parent 47e8ad7 commit 7eabce8

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

.changeset/hip-jeans-provide.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+
chore: improve the performance of set_text for single expressions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export function build_template_literal(values, visit, state) {
6464
)
6565
);
6666
expressions.push(b.call('$.get', id));
67+
} else if (values.length === 1) {
68+
// If we have a single expression, then pass that in directly to possibly avoid doing
69+
// extra work in the template_effect (instead we do the work in set_text).
70+
return { value: visit(node.expression, state), has_state, has_call };
6771
} else {
6872
expressions.push(b.logical('??', visit(node.expression, state), b.literal('')));
6973
}

packages/svelte/src/internal/client/render.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export function set_text(text, value) {
5151
// @ts-expect-error
5252
if (value !== (text.__t ??= text.nodeValue)) {
5353
// @ts-expect-error
54-
text.nodeValue = text.__t = value;
54+
text.__t = value;
55+
text.nodeValue = value == null ? '' : value + '';
5556
}
5657
}
5758

packages/svelte/tests/migrate/samples/each-block-const/output.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
const { foo } = x();
33
</script>
44

5-
{#each foo as f}{/each}
5+
{#each foo as f}{/each}

packages/svelte/tests/snapshot/samples/purity/_expected/client/index.svelte.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export default function Purity($$anchor) {
1111
var fragment = root();
1212
var p = $.first_child(fragment);
1313

14-
p.textContent = `${Math.max(min, Math.min(max, number)) ?? ""}`;
14+
p.textContent = Math.max(min, Math.min(max, number));
1515

1616
var p_1 = $.sibling($.sibling(p, true));
1717

18-
p_1.textContent = `${location.href ?? ""}`;
18+
p_1.textContent = location.href;
1919

2020
var node = $.sibling($.sibling(p_1, true));
2121

0 commit comments

Comments
 (0)