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 <rich.harris@vercel.com>
pull/12786/head
ottomated 3 months ago committed by GitHub
parent 9f28503bef
commit d6e26c0953
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
perf: speed up $.exclude_from_object

@ -276,15 +276,19 @@ function _extract_paths(assignments = [], param, expression, update_expression,
const rest_expression = (object) => { const rest_expression = (object) => {
/** @type {ESTree.Expression[]} */ /** @type {ESTree.Expression[]} */
const props = []; const props = [];
for (const p of param.properties) { for (const p of param.properties) {
if (p.type === 'Property' && p.key.type !== 'PrivateIdentifier') { if (p.type === 'Property' && p.key.type !== 'PrivateIdentifier') {
if (p.key.type === 'Identifier' && !p.computed) { if (p.key.type === 'Identifier' && !p.computed) {
props.push(b.literal(p.key.name)); props.push(b.literal(p.key.name));
} else if (p.key.type === 'Literal') {
props.push(b.literal(String(p.key.value)));
} else { } else {
props.push(p.key); props.push(b.call('String', p.key));
} }
} }
} }
return b.call('$.exclude_from_object', expression(object), b.array(props)); return b.call('$.exclude_from_object', expression(object), b.array(props));
}; };

@ -991,12 +991,16 @@ export function update_pre(signal, d = 1) {
* @returns {Record<string, unknown>} * @returns {Record<string, unknown>}
*/ */
export function exclude_from_object(obj, keys) { export function exclude_from_object(obj, keys) {
obj = { ...obj }; /** @type {Record<string, unknown>} */
let key; var result = {};
for (key of keys) {
delete obj[key]; for (var key in obj) {
if (!keys.includes(key)) {
result[key] = obj[key];
}
} }
return obj;
return result;
} }
/** /**

@ -9,18 +9,21 @@ export default test({
quote: 'q1', quote: 'q1',
'wrong-quote': 'wq1', 'wrong-quote': 'wq1',
16: '16', 16: '16',
17: '17',
class: 'class' class: 'class'
}, },
{ {
quote: 'q2', quote: 'q2',
'wrong-quote': 'wq2', 'wrong-quote': 'wq2',
16: 'sixteen', 16: 'sixteen',
17: 'seventeen',
class: 'glass' class: 'glass'
}, },
{ {
quote: 'q3', quote: 'q3',
'wrong-quote': 'wq3', 'wrong-quote': 'wq3',
16: 'seize', 16: 'seize',
17: 'dix-sept',
class: 'mass' class: 'mass'
} }
] ]
@ -28,19 +31,19 @@ export default test({
}, },
html: ` html: `
<p class="class">Quote: q1, Wrong Quote: wq1, 16: 16</p> <p class="class">Quote: q1, Wrong Quote: wq1, 16: 16, 17: 17</p>
<p class="glass">Quote: q2, Wrong Quote: wq2, 16: sixteen</p> <p class="glass">Quote: q2, Wrong Quote: wq2, 16: sixteen, 17: seventeen</p>
<p class="mass">Quote: q3, Wrong Quote: wq3, 16: seize</p> <p class="mass">Quote: q3, Wrong Quote: wq3, 16: seize, 17: dix-sept</p>
`, `,
test({ assert, component, target }) { test({ assert, component, target }) {
component.objectsArray = [ component.objectsArray = [
{ quote: 'new-quote', 'wrong-quote': 'wq4', 16: 'ten+six', role: 'role' } { quote: 'new-quote', 'wrong-quote': 'wq4', 16: 'ten+six', 17: 'ten+seven', role: 'role' }
]; ];
assert.htmlEqual( assert.htmlEqual(
target.innerHTML, target.innerHTML,
` `
<p role="role">Quote: new-quote, Wrong Quote: wq4, 16: ten+six</p> <p role="role">Quote: new-quote, Wrong Quote: wq4, 16: ten+six, 17: ten+seven</p>
` `
); );
} }

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

Loading…
Cancel
Save