Skip to content

Commit faf1822

Browse files
committed
optimise for the common case
1 parent 941f266 commit faf1822

File tree

13 files changed

+107
-119
lines changed

13 files changed

+107
-119
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export function template_to_functions(items) {
1919
function build(item) {
2020
switch (item.type) {
2121
case 'element': {
22-
const element = b.object([b.prop('init', b.id('e'), b.literal(item.name))]);
22+
const element = b.array([b.literal(item.name)]);
2323

24-
const attributes = b.prop('init', b.id('p'), b.object([]));
24+
const attributes = b.object([]);
2525

2626
for (const key in item.attributes) {
2727
const value = item.attributes[key];
2828

29-
attributes.value.properties.push(
29+
attributes.properties.push(
3030
b.prop(
3131
'init',
3232
b.key(fix_attribute_casing(key)),
@@ -35,13 +35,13 @@ function build(item) {
3535
);
3636
}
3737

38-
if (attributes.value.properties.length > 0) {
39-
element.properties.push(attributes);
38+
if (attributes.properties.length > 0 || item.children.length > 0) {
39+
element.elements.push(attributes.properties.length > 0 ? attributes : b.null);
4040
}
4141

4242
if (item.children.length > 0) {
4343
const children = item.children.map(build);
44-
element.properties.push(b.prop('init', b.id('c'), b.array(children)));
44+
element.elements.push(...children);
4545

4646
// special case — strip leading newline from `<pre>` and `<textarea>`
4747
if (item.name === 'pre' || item.name === 'textarea') {
@@ -59,7 +59,7 @@ function build(item) {
5959
}
6060

6161
case 'anchor': {
62-
return item.data ? b.array([b.literal(item.data)]) : null;
62+
return item.data ? b.array([b.literal(`// ${item.data}`)]) : null;
6363
}
6464

6565
case 'text': {

packages/svelte/src/internal/client/dom/template.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** @import { Effect, TemplateNode } from '#client' */
2+
/** @import { TemplateStructure } from './types' */
23
import { hydrate_next, hydrate_node, hydrating, set_hydrate_node } from './hydration.js';
34
import {
45
create_text,
@@ -76,46 +77,44 @@ export function template(content, flags) {
7677
return clone;
7778
};
7879
}
79-
8080
/**
81-
* @typedef {{e: string, p: Record<string, string>, c: Array<TemplateStructure>} | undefined | string | [string]} TemplateStructure
82-
*/
83-
84-
/**
85-
* @param {Array<TemplateStructure>} structure
81+
* @param {TemplateStructure[]} structure
8682
* @param {NAMESPACE_SVG | NAMESPACE_MATHML | undefined} [ns]
8783
*/
8884
function structure_to_fragment(structure, ns) {
8985
var fragment = create_fragment();
9086

9187
for (var item of structure) {
92-
if (item === undefined || Array.isArray(item)) {
93-
fragment.append(create_comment(item ? item[0] : ''));
88+
if (typeof item === 'string') {
89+
fragment.append(create_text(item));
9490
continue;
9591
}
9692

97-
if (typeof item === 'string') {
98-
fragment.append(create_text(item));
93+
// if `preserveComments === true`, comments are represented as `['// <data>']`
94+
if (item === undefined || item[0][0] === '/') {
95+
fragment.append(create_comment(item ? item[0].slice(3) : ''));
9996
continue;
10097
}
10198

99+
const [name, attributes, ...children] = item;
100+
102101
/** @type {NAMESPACE_SVG | NAMESPACE_MATHML | undefined} */
103-
let namespace = item.e === 'svg' ? NAMESPACE_SVG : item.e === 'math' ? NAMESPACE_MATHML : ns;
102+
let namespace = name === 'svg' ? NAMESPACE_SVG : name === 'math' ? NAMESPACE_MATHML : ns;
104103

105-
var element = create_element(item.e, namespace, item.p?.is);
104+
var element = create_element(name, namespace, attributes?.is);
106105

107-
for (var key in item.p) {
108-
set_attribute(element, key, item.p[key]);
106+
for (var key in attributes) {
107+
set_attribute(element, key, attributes[key]);
109108
}
110109

111-
if (item.c) {
110+
if (children.length > 0) {
112111
var target =
113112
element.tagName === 'TEMPLATE'
114113
? /** @type {HTMLTemplateElement} */ (element).content
115114
: element;
116115

117116
target.append(
118-
structure_to_fragment(item.c, element.tagName === 'foreignObject' ? undefined : namespace)
117+
structure_to_fragment(children, element.tagName === 'foreignObject' ? undefined : namespace)
119118
);
120119
}
121120

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type TemplateStructure =
2+
| string
3+
| undefined
4+
| [string, Record<string, string> | undefined, ...TemplateStructure[]];

packages/svelte/tests/snapshot/samples/await-block-scope/_expected/client-functional/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function increment(_, counter) {
55
counter.count += 1;
66
}
77

8-
var root = $.template_fn([{ e: 'button', c: [' '] }, ' ', , ' '], 1);
8+
var root = $.template_fn([['button', null, ' '], ' ', , ' '], 1);
99

1010
export default function Await_block_scope($$anchor) {
1111
let counter = $.proxy({ count: 0 });

packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client-functional/main.svelte.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import * as $ from 'svelte/internal/client';
33

44
var root = $.template_fn(
55
[
6-
{ e: 'div' },
6+
['div'],
77
' ',
8-
{ e: 'svg' },
8+
['svg'],
99
' ',
10-
{ e: 'custom-element' },
10+
['custom-element'],
1111
' ',
12-
{ e: 'div' },
12+
['div'],
1313
' ',
14-
{ e: 'svg' },
14+
['svg'],
1515
' ',
16-
{ e: 'custom-element' }
16+
['custom-element']
1717
],
1818
3
1919
);

packages/svelte/tests/snapshot/samples/each-index-non-null/_expected/client-functional/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'svelte/internal/disclose-version';
22
import 'svelte/internal/flags/legacy';
33
import * as $ from 'svelte/internal/client';
44

5-
var root_1 = $.template_fn([{ e: 'p' }]);
5+
var root_1 = $.template_fn([['p']]);
66

77
export default function Each_index_non_null($$anchor) {
88
var fragment = $.comment();

packages/svelte/tests/snapshot/samples/hello-world/_expected/client-functional/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'svelte/internal/disclose-version';
22
import 'svelte/internal/flags/legacy';
33
import * as $ from 'svelte/internal/client';
44

5-
var root = $.template_fn([{ e: 'h1', c: ['hello world'] }]);
5+
var root = $.template_fn([['h1', null, 'hello world']]);
66

77
export default function Hello_world($$anchor) {
88
var h1 = root();

packages/svelte/tests/snapshot/samples/hmr/_expected/client-functional/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'svelte/internal/disclose-version';
22
import 'svelte/internal/flags/legacy';
33
import * as $ from 'svelte/internal/client';
44

5-
var root = $.template_fn([{ e: 'h1', c: ['hello world'] }]);
5+
var root = $.template_fn([['h1', null, 'hello world']]);
66

77
function Hmr($$anchor) {
88
var h1 = root();

packages/svelte/tests/snapshot/samples/nullish-coallescence-omittance/_expected/client-functional/index.svelte.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ var on_click = (_, count) => $.update(count);
55

66
var root = $.template_fn(
77
[
8-
{ e: 'h1' },
8+
['h1'],
99
' ',
10-
{ e: 'b' },
10+
['b'],
1111
' ',
12-
{ e: 'button', c: [' '] },
12+
['button', null, ' '],
1313
' ',
14-
{ e: 'h1' }
14+
['h1']
1515
],
1616
1
1717
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'svelte/internal/disclose-version';
22
import 'svelte/internal/flags/legacy';
33
import * as $ from 'svelte/internal/client';
44

5-
var root = $.template_fn([{ e: 'p' }, ' ', { e: 'p' }, ' ', ,], 1);
5+
var root = $.template_fn([['p'], ' ', ['p'], ' ', ,], 1);
66

77
export default function Purity($$anchor) {
88
var fragment = root();

0 commit comments

Comments
 (0)