Skip to content

Batch updates in flushSync#5175

Open
JoviDeCroock wants to merge 1 commit into
mainfrom
flush-sync-batch-main
Open

Batch updates in flushSync#5175
JoviDeCroock wants to merge 1 commit into
mainfrom
flush-sync-batch-main

Conversation

@JoviDeCroock

Copy link
Copy Markdown
Member

Summary

Main mirror of #5173.

Make preact/compat's flushSync batch updates performed inside its callback.

Previously, flushSync temporarily configured options.debounceRendering to execute every scheduled render immediately. This made updates synchronous, but it also caused each state setter to render independently:

flushSync(() => {
	setA(1);
	setB(1);
});

The example above could render twice instead of batching both updates into one synchronous commit.

This change captures the scheduled render callback while the user callback runs, then flushes it once before returning. The previous debounce function is restored in a finally block.

Results

Measured on the v10.x branch (#5173) with Octane's nested update benchmark:

Operation Before After Change
Batched sweep, ancestor-first 0.322 ms 0.081 ms −75%
Batched sweep, descendant-first 0.301 ms 0.079 ms −74%
Suite geomean 0.161 ms 0.116 ms −28%

Non-batched update timings remain unchanged. compat.mjs grows by 14 B brotli.

The batching test is mirrored as well (unstable_batchedUpdates.test.js promoted to .jsx).

Previously, flushSync temporarily configured options.debounceRendering
to execute every scheduled render immediately. This made updates
synchronous, but it also caused each state setter to render
independently instead of batching all updates performed inside the
callback into one synchronous commit.

Capture the scheduled render callback while the user callback runs, then
flush it once before returning. The previous debounce function is
restored in a finally block.

Mirrors #5173 (v10.x).
@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown

📊 Tachometer Benchmark Results

Summary

duration

  • create10k: unsure 🔍 -1% - +0% (-4.40ms - +2.25ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -0% - +1% (-0.01ms - +0.12ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -2% - +2% (-1.05ms - +1.27ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -2% - +2% (-0.36ms - +0.35ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -1% - +1% (-0.51ms - +0.31ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -2% - +6% (-0.04ms - +0.12ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -1% - +1% (-0.42ms - +0.26ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -2% - +5% (-0.47ms - +1.52ms)
    preact-local vs preact-main

usedJSHeapSize

  • create10k: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -0% - +0% (-0.00ms - +0.01ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -2% - +4% (-0.11ms - +0.22ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -0% - +0% (-0.01ms - +0.00ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -2% - +4% (-0.02ms - +0.04ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -0% - +0% (-0.00ms - +0.01ms)
    preact-local vs preact-main

Results

create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local839.16ms - 843.77ms-unsure 🔍
-1% - +0%
-4.40ms - +2.25ms
preact-main840.15ms - 844.94msunsure 🔍
-0% - +1%
-2.25ms - +4.40ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.09ms - 19.09ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-main19.09ms - 19.09msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.54ms - 16.66ms-unsure 🔍
-0% - +1%
-0.01ms - +0.12ms
preact-main16.52ms - 16.56msunsure 🔍
-1% - +0%
-0.12ms - +0.01ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.57ms - 1.57ms-unsure 🔍
-0% - +0%
-0.00ms - +0.01ms
preact-main1.57ms - 1.57msunsure 🔍
-0% - +0%
-0.01ms - +0.00ms
-
hydrate1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local55.71ms - 57.24ms-unsure 🔍
-2% - +2%
-1.05ms - +1.27ms
preact-main55.49ms - 57.24msunsure 🔍
-2% - +2%
-1.27ms - +1.05ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local5.04ms - 5.29ms-unsure 🔍
-2% - +4%
-0.11ms - +0.22ms
preact-main5.01ms - 5.21msunsure 🔍
-4% - +2%
-0.22ms - +0.11ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.33ms - 16.64ms-unsure 🔍
-2% - +2%
-0.36ms - +0.35ms
preact-main16.16ms - 16.81msunsure 🔍
-2% - +2%
-0.35ms - +0.36ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.75ms - 3.76ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-main3.75ms - 3.76msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #5715
  • Commit: f109716

duration

VersionAvg timevs preact-localvs preact-main
preact-local49.32ms - 49.88ms-unsure 🔍
-1% - +1%
-0.51ms - +0.31ms
preact-main49.39ms - 49.99msunsure 🔍
-1% - +1%
-0.31ms - +0.51ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.03ms - 3.03ms-unsure 🔍
-0% - +0%
-0.01ms - +0.00ms
preact-main3.03ms - 3.03msunsure 🔍
-0% - +0%
-0.00ms - +0.01ms
-

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local25.03ms - 25.68ms-unsure 🔍
-1% - +2%
-0.31ms - +0.58ms
preact-main24.92ms - 25.52msunsure 🔍
-2% - +1%
-0.58ms - +0.31ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local27.65ms - 28.57ms-unsure 🔍
-3% - +1%
-0.86ms - +0.43ms
preact-main27.87ms - 28.78msunsure 🔍
-2% - +3%
-0.43ms - +0.86ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local24.96ms - 25.76ms-unsure 🔍
-1% - +3%
-0.37ms - +0.73ms
preact-main24.80ms - 25.56msunsure 🔍
-3% - +1%
-0.73ms - +0.37ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local22.27ms - 22.45ms-unsure 🔍
-0% - +1%
-0.09ms - +0.16ms
preact-main22.24ms - 22.41msunsure 🔍
-1% - +0%
-0.16ms - +0.09ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local23.32ms - 24.28ms-unsure 🔍
-4% - +2%
-0.90ms - +0.47ms
preact-main23.53ms - 24.51msunsure 🔍
-2% - +4%
-0.47ms - +0.90ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local17.65ms - 17.97ms-unsure 🔍
-1% - +2%
-0.14ms - +0.31ms
preact-main17.57ms - 17.89msunsure 🔍
-2% - +1%
-0.31ms - +0.14ms
-
text-update
  • Browser: chrome-headless
  • Sample size: 220
  • Built by: CI #5715
  • Commit: f109716

duration

VersionAvg timevs preact-localvs preact-main
preact-local1.92ms - 2.03ms-unsure 🔍
-2% - +6%
-0.04ms - +0.12ms
preact-main1.88ms - 1.98msunsure 🔍
-6% - +2%
-0.12ms - +0.04ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.03ms - 1.08ms-unsure 🔍
-2% - +4%
-0.02ms - +0.04ms
preact-main1.02ms - 1.06msunsure 🔍
-4% - +2%
-0.04ms - +0.02ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local31.94ms - 32.50ms-unsure 🔍
-1% - +1%
-0.42ms - +0.26ms
preact-main32.11ms - 32.48msunsure 🔍
-1% - +1%
-0.26ms - +0.42ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.28ms - 1.28ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-main1.28ms - 1.28msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local31.15ms - 32.58ms-unsure 🔍
-2% - +5%
-0.47ms - +1.52ms
preact-main30.64ms - 32.03msunsure 🔍
-5% - +1%
-1.52ms - +0.47ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.99ms - 3.00ms-unsure 🔍
-0% - +0%
-0.00ms - +0.01ms
preact-main2.98ms - 3.00msunsure 🔍
-0% - +0%
-0.01ms - +0.00ms
-

tachometer-reporter-action v2 for CI

@github-actions

Copy link
Copy Markdown

Size Change: +12 B (+0.08%)

Total Size: 15.8 kB

📦 View Changed
Filename Size Change
compat/dist/compat.mjs 3.71 kB +12 B (+0.32%)
ℹ️ View Unchanged
Filename Size
debug/dist/debug.mjs 3.89 kB
devtools/dist/devtools.mjs 274 B
dist/preact.mjs 5.01 kB
hooks/dist/hooks.mjs 1.55 kB
jsx-runtime/dist/jsxRuntime.mjs 861 B
test-utils/dist/testUtils.mjs 473 B

compressed-size-action

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 99.341% (+0.8%) from 98.492% — flush-sync-batch-main into main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants