diff --git a/.changeset/polite-melons-tickle.md b/.changeset/polite-melons-tickle.md new file mode 100644 index 0000000000..42967ea936 --- /dev/null +++ b/.changeset/polite-melons-tickle.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +chore: avoid microtasks when flushing sync diff --git a/benchmarking/benchmarks/reactivity/index.js b/benchmarking/benchmarks/reactivity/index.js index 58b3f5cb29..5dbfce0bb9 100644 --- a/benchmarking/benchmarks/reactivity/index.js +++ b/benchmarking/benchmarks/reactivity/index.js @@ -24,32 +24,32 @@ import { // Not all tests are the same, and many parts have been tweaked to capture different data. export const reactivity_benchmarks = [ - sbench_create_signals, - sbench_create_0to1, - sbench_create_1to1, - sbench_create_2to1, - sbench_create_4to1, - sbench_create_1000to1, - sbench_create_1to2, - sbench_create_1to4, - sbench_create_1to8, - sbench_create_1to1000, - kairo_avoidable_owned, - kairo_avoidable_unowned, - kairo_broad_owned, - kairo_broad_unowned, - kairo_deep_owned, - kairo_deep_unowned, + // sbench_create_signals, + // sbench_create_0to1, + // sbench_create_1to1, + // sbench_create_2to1, + // sbench_create_4to1, + // sbench_create_1000to1, + // sbench_create_1to2, + // sbench_create_1to4, + // sbench_create_1to8, + // sbench_create_1to1000, + // kairo_avoidable_owned, + // kairo_avoidable_unowned, + // kairo_broad_owned, + // kairo_broad_unowned, + // kairo_deep_owned, + // kairo_deep_unowned, kairo_diamond_owned, kairo_diamond_unowned, - kairo_triangle_owned, - kairo_triangle_unowned, - kairo_mux_owned, - kairo_mux_unowned, - kairo_repeated_owned, - kairo_repeated_unowned, - kairo_unstable_owned, - kairo_unstable_unowned, - mol_bench_owned, - mol_bench_unowned + // kairo_triangle_owned, + // kairo_triangle_unowned, + // kairo_mux_owned, + // kairo_mux_unowned, + // kairo_repeated_owned, + // kairo_repeated_unowned, + // kairo_unstable_owned, + // kairo_unstable_unowned, + // mol_bench_owned, + // mol_bench_unowned ]; diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 7a926bf624..6279c6bdda 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -47,6 +47,7 @@ const handled_errors = new WeakSet(); let is_throwing_error = false; let is_flushing = false; +let is_flushing_sync = false; /** @type {Effect | null} */ let last_scheduled_effect = null; @@ -734,7 +735,9 @@ function flush_queued_effects(effects) { export function schedule_effect(signal) { if (!is_flushing) { is_flushing = true; - queueMicrotask(flush_queued_root_effects); + if (!is_flushing_sync) { + queueMicrotask(flush_queued_root_effects); + } } var effect = (last_scheduled_effect = signal); @@ -818,23 +821,30 @@ function process_effects(root) { * @returns {T} */ export function flushSync(fn) { - var result; - - if (fn) { - is_flushing = true; - flush_queued_root_effects(); - result = fn(); - } + var previously_flushing_sync = is_flushing_sync; - flush_tasks(); + try { + var result; + is_flushing_sync = true; - while (queued_root_effects.length > 0) { - is_flushing = true; - flush_queued_root_effects(); + if (fn) { + is_flushing = true; + flush_queued_root_effects(); + result = fn(); + } + flush_tasks(); + + while (queued_root_effects.length > 0) { + is_flushing = true; + flush_queued_root_effects(); + flush_tasks(); + } + + return /** @type {T} */ (result); + } finally { + is_flushing_sync = previously_flushing_sync; } - - return /** @type {T} */ (result); } /**