diff --git a/CHANGELOG.md b/CHANGELOG.md index a906065883e1..899e0d5b8389 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Fix binding to module-level variables ([#4086](https://github.com/sveltejs/svelte/issues/4086)) * Disallow attribute/prop names from matching two-way-bound names or `{shorthand}` attribute/prop names ([#4325](https://github.com/sveltejs/svelte/issues/4325)) +* Improve performance of `flush()` by not using `.shift()` ([#4356](https://github.com/sveltejs/svelte/pull/4356)) * Fix code generation error with precedence of arrow functions ([#4384](https://github.com/sveltejs/svelte/issues/4384)) ## 3.18.1 diff --git a/src/runtime/internal/scheduler.ts b/src/runtime/internal/scheduler.ts index 1ce255b21772..b0db71035a98 100644 --- a/src/runtime/internal/scheduler.ts +++ b/src/runtime/internal/scheduler.ts @@ -31,18 +31,23 @@ export function add_flush_callback(fn) { flush_callbacks.push(fn); } +let flushing = false; const seen_callbacks = new Set(); export function flush() { + if (flushing) return; + flushing = true; do { // first, call beforeUpdate functions // and update components - while (dirty_components.length) { - const component = dirty_components.shift(); + for (let i = 0; i < dirty_components.length; i += 1) { + const component = dirty_components[i]; set_current_component(component); update(component.$$); } + dirty_components.length = 0; + while (binding_callbacks.length) binding_callbacks.pop()(); // then, once components are updated, call @@ -67,6 +72,7 @@ export function flush() { } update_scheduled = false; + flushing = false; seen_callbacks.clear(); }