perf: O(n²)→O(n) Map lookups for legacy `$:` reactive statements

Use Map.get/has instead of spread+find/includes when ordering and
emitting legacy reactive statements. n is the number of `$:` statements
per component.
pull/18602/head
ljodea 4 days ago
parent 44a7813730
commit 22247150c8

@ -0,0 +1,5 @@
---
'svelte': patch
---
perf: O(n²)→O(n) Map lookups for legacy `$:` reactive statement ordering

@ -1314,7 +1314,8 @@ function order_reactive_statements(unsorted_reactive_declarations) {
* @returns
*/
const add_declaration = (node, declaration) => {
if ([...reactive_declarations.values()].includes(declaration)) return;
// Visited set: each ReactiveStatement is stored under exactly one LabeledStatement node
if (reactive_declarations.has(node)) return;
for (const binding of declaration.dependencies) {
if (declaration.assignments.has(binding)) continue;

@ -247,11 +247,11 @@ export function client_component(analysis, options) {
}
for (const [node] of analysis.reactive_statements) {
const statement = [...state.legacy_reactive_statements].find(([n]) => n === node);
const statement = state.legacy_reactive_statements.get(node);
if (statement === undefined) {
throw new Error('Could not find reactive statement');
}
instance.body.push(statement[1]);
instance.body.push(statement);
}
if (analysis.reactive_statements.size > 0) {

@ -148,7 +148,7 @@ export function server_component(analysis, options) {
const legacy_reactive_declarations = [];
for (const [node] of analysis.reactive_statements) {
const statement = [...state.legacy_reactive_statements].find(([n]) => n === node);
const statement = state.legacy_reactive_statements.get(node);
if (statement === undefined) {
throw new Error('Could not find reactive statement');
}
@ -165,7 +165,7 @@ export function server_component(analysis, options) {
}
}
instance.body.push(statement[1]);
instance.body.push(statement);
}
if (legacy_reactive_declarations.length > 0) {

Loading…
Cancel
Save