Skip to content

Commit d6e26c0

Browse files
chore: speed up exclude_from_object (#12783)
* speed up exclude_from_object * changeset * convert prop names to string at compile time * faster still --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 9f28503 commit d6e26c0

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

.changeset/brown-insects-float.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
perf: speed up $.exclude_from_object

packages/svelte/src/compiler/utils/ast.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,19 @@ function _extract_paths(assignments = [], param, expression, update_expression,
276276
const rest_expression = (object) => {
277277
/** @type {ESTree.Expression[]} */
278278
const props = [];
279+
279280
for (const p of param.properties) {
280281
if (p.type === 'Property' && p.key.type !== 'PrivateIdentifier') {
281282
if (p.key.type === 'Identifier' && !p.computed) {
282283
props.push(b.literal(p.key.name));
284+
} else if (p.key.type === 'Literal') {
285+
props.push(b.literal(String(p.key.value)));
283286
} else {
284-
props.push(p.key);
287+
props.push(b.call('String', p.key));
285288
}
286289
}
287290
}
291+
288292
return b.call('$.exclude_from_object', expression(object), b.array(props));
289293
};
290294

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -991,12 +991,16 @@ export function update_pre(signal, d = 1) {
991991
* @returns {Record<string, unknown>}
992992
*/
993993
export function exclude_from_object(obj, keys) {
994-
obj = { ...obj };
995-
let key;
996-
for (key of keys) {
997-
delete obj[key];
994+
/** @type {Record<string, unknown>} */
995+
var result = {};
996+
997+
for (var key in obj) {
998+
if (!keys.includes(key)) {
999+
result[key] = obj[key];
1000+
}
9981001
}
999-
return obj;
1002+
1003+
return result;
10001004
}
10011005

10021006
/**

packages/svelte/tests/runtime-legacy/samples/each-block-destructured-object-literal-rest/_config.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,41 @@ export default test({
99
quote: 'q1',
1010
'wrong-quote': 'wq1',
1111
16: '16',
12+
17: '17',
1213
class: 'class'
1314
},
1415
{
1516
quote: 'q2',
1617
'wrong-quote': 'wq2',
1718
16: 'sixteen',
19+
17: 'seventeen',
1820
class: 'glass'
1921
},
2022
{
2123
quote: 'q3',
2224
'wrong-quote': 'wq3',
2325
16: 'seize',
26+
17: 'dix-sept',
2427
class: 'mass'
2528
}
2629
]
2730
};
2831
},
2932

3033
html: `
31-
<p class="class">Quote: q1, Wrong Quote: wq1, 16: 16</p>
32-
<p class="glass">Quote: q2, Wrong Quote: wq2, 16: sixteen</p>
33-
<p class="mass">Quote: q3, Wrong Quote: wq3, 16: seize</p>
34+
<p class="class">Quote: q1, Wrong Quote: wq1, 16: 16, 17: 17</p>
35+
<p class="glass">Quote: q2, Wrong Quote: wq2, 16: sixteen, 17: seventeen</p>
36+
<p class="mass">Quote: q3, Wrong Quote: wq3, 16: seize, 17: dix-sept</p>
3437
`,
3538

3639
test({ assert, component, target }) {
3740
component.objectsArray = [
38-
{ quote: 'new-quote', 'wrong-quote': 'wq4', 16: 'ten+six', role: 'role' }
41+
{ quote: 'new-quote', 'wrong-quote': 'wq4', 16: 'ten+six', 17: 'ten+seven', role: 'role' }
3942
];
4043
assert.htmlEqual(
4144
target.innerHTML,
4245
`
43-
<p role="role">Quote: new-quote, Wrong Quote: wq4, 16: ten+six</p>
46+
<p role="role">Quote: new-quote, Wrong Quote: wq4, 16: ten+six, 17: ten+seven</p>
4447
`
4548
);
4649
}

packages/svelte/tests/runtime-legacy/samples/each-block-destructured-object-literal-rest/main.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
export let objectsArray;
33
</script>
44

5-
{#each objectsArray as { "quote": quotedProp, "wrong-quote": wrongQuote, 16: sixteen, ...props } }
6-
<p {...props}>Quote: {quotedProp}, Wrong Quote: {wrongQuote}, 16: {sixteen}</p>
5+
{#each objectsArray as { "quote": quotedProp, "wrong-quote": wrongQuote, 16: sixteen, [10 + 7]: seventeen, ...props }}
6+
<p {...props}>Quote: {quotedProp}, Wrong Quote: {wrongQuote}, 16: {sixteen}, 17: {seventeen}</p>
77
{/each}

0 commit comments

Comments
 (0)