Skip to content

Add the constructor in when cloning VNodes - #5195

Merged
JoviDeCroock merged 3 commits into
mainfrom
JoviDeCroock/issue-5109
Aug 6, 2026
Merged

Add the constructor in when cloning VNodes#5195
JoviDeCroock merged 3 commits into
mainfrom
JoviDeCroock/issue-5109

Conversation

@JoviDeCroock

@JoviDeCroock JoviDeCroock commented Aug 6, 2026

Copy link
Copy Markdown
Member

Fixes #5109
Supersedes #5078

This fixes the SES setup and has the nice benefit of positively affecting perf for low byte-size

JoviDeCroock and others added 2 commits August 5, 2026 17:27
Object.assign({}, vnode) in renderComponent(), cloneNode in
src/diff/index.js, and detachedClone in compat/src/suspense.js
tries to copy a vnode's `constructor: undefined` own property via
[[Set]]. Under environments where `Object.prototype.constructor`
is a non-writable data property (e.g. raw
`Object.freeze(Object.prototype)` or hardenedjs configured with
`overrideTaming: 'min'`), the assignment trips the override
mistake and fails with:

  TypeError: Cannot assign to read only property 'constructor'

Initial render works because createVNode builds vnodes with an
object literal ([[CreateDataProperty]], not Set), but any
setState/forceUpdate-triggered re-render goes through
Object.assign({}, oldVNode) and trips the override mistake.

Note: hardening configs that tame the override mistake by
replacing `Object.prototype.constructor` with an accessor pair
(SES default, Node's `--frozen-intrinsics`) do NOT exhibit this
bug — but the fix is still required for the untamed configs
above. The added Node subprocess test guards against future
changes to that taming.

Fixes #5109

Co-authored-by: kumavis <kumavis@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Inline `assign({ constructor: UNDEFINED }, vnode)` at the three clone
sites instead of routing them through a shared `cloneVNode` helper.
compat can't reach a core-internal helper across the `preact` external
boundary, so the helper ends up duplicated into the compat bundle:
4555 B / 3518 B brotli (core/compat) with the helper vs 4550 B / 3510 B
inline, against 4547 B / 3490 B on main. Net cost of the fix is +3 B
core, +20 B compat.

Drop the `constructor`-first reordering of createVNode's object literal.
`%HaveSameMap` shows a cloned vnode never shares a hidden class with
createVNode's literal, with or without the reorder — V8 gives literals a
different in-object slot count than transition-built objects — so it
buys no shape identity. Clone-vs-clone stays monomorphic either way, and
a microbench (node 22, best of 7, 2M clones) puts the seeded clone at
374 ms against 428 ms for `assign({}, v)`, ~13% faster with or without
the reorder. Leaving the key order alone also keeps the createElement
key-order test untouched.

Replace the node tests with browser tests that exercise the actual
failure. `frozen-intrinsics.test.js` cannot reproduce the bug — node
tames `Object.prototype.constructor` into an accessor pair under
`--frozen-intrinsics`, as its own comment notes — and it reaches for
esbuild, which is not a declared devDependency;
`object-prototype-freeze.test.js` covered the clone helper rather than a
render. The new tests instead redefine `Object.prototype.constructor` as
non-writable (the narrowest reproduction of the override mistake, and
reversible, so the shared browser realm isn't poisoned) and drive
setState, a Fragment-returning component (cloneNode) and Suspense
(detachedClone). All three fail on main and pass here.

Checked end to end in Chromium under real SES `lockdown()` and under
bare `Object.freeze(Object.prototype)`: hydrate, class + hooks +
context, memo/forwardRef, portals, keyed reordering, controlled inputs,
error boundaries and lazy/Suspense all run clean, so this clone was the
only hardening hazard on those paths.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown

📊 Tachometer Benchmark Results

Summary

duration

  • create10k: unsure 🔍 -0% - +1% (-3.24ms - +8.67ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -0% - +0% (-0.04ms - +0.05ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -2% - +1% (-1.34ms - +0.67ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -4% - +5% (-0.67ms - +0.80ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -0% - +4% (-0.07ms - +2.40ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -0% - +7% (-0.01ms - +0.13ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -0% - +1% (-0.14ms - +0.48ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -4% - +4% (-1.23ms - +1.20ms)
    preact-local vs preact-main

usedJSHeapSize

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

Results

create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local850.86ms - 859.94ms-unsure 🔍
-0% - +1%
-3.24ms - +8.67ms
preact-main848.84ms - 856.54msunsure 🔍
-1% - +0%
-8.67ms - +3.24ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.24ms - 19.27ms-unsure 🔍
-0% - +0%
-0.02ms - +0.02ms
preact-main19.24ms - 19.27msunsure 🔍
-0% - +0%
-0.02ms - +0.02ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.51ms - 16.59ms-unsure 🔍
-0% - +0%
-0.04ms - +0.05ms
preact-main16.53ms - 16.57msunsure 🔍
-0% - +0%
-0.05ms - +0.04ms
-

usedJSHeapSize

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

duration

VersionAvg timevs preact-localvs preact-main
preact-local57.39ms - 58.79ms-unsure 🔍
-2% - +1%
-1.34ms - +0.67ms
preact-main57.70ms - 59.14msunsure 🔍
-1% - +2%
-0.67ms - +1.34ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local4.99ms - 5.34ms-unsure 🔍
-4% - +4%
-0.22ms - +0.23ms
preact-main5.02ms - 5.30msunsure 🔍
-4% - +4%
-0.23ms - +0.22ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.24ms - 17.15ms-unsure 🔍
-4% - +5%
-0.67ms - +0.80ms
preact-main16.06ms - 17.21msunsure 🔍
-5% - +4%
-0.80ms - +0.67ms
-

usedJSHeapSize

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

duration

VersionAvg timevs preact-localvs preact-main
preact-local58.21ms - 60.01ms-unsure 🔍
-0% - +4%
-0.07ms - +2.40ms
preact-main57.10ms - 58.78msunsure 🔍
-4% - +0%
-2.40ms - +0.07ms
-

usedJSHeapSize

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

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local26.33ms - 26.87ms-faster ✔
0% - 3%
0.10ms - 0.90ms
preact-main26.80ms - 27.39msslower ❌
0% - 3%
0.10ms - 0.90ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local32.73ms - 33.83ms-unsure 🔍
-1% - +4%
-0.28ms - +1.23ms
preact-main32.29ms - 33.32msunsure 🔍
-4% - +1%
-1.23ms - +0.28ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local31.52ms - 32.91ms-unsure 🔍
-4% - +2%
-1.23ms - +0.68ms
preact-main31.83ms - 33.15msunsure 🔍
-2% - +4%
-0.68ms - +1.23ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local26.16ms - 26.40ms-unsure 🔍
-1% - +0%
-0.38ms - +0.13ms
preact-main26.18ms - 26.63msunsure 🔍
-0% - +1%
-0.13ms - +0.38ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local26.51ms - 27.49ms-unsure 🔍
-2% - +3%
-0.60ms - +0.87ms
preact-main26.32ms - 27.41msunsure 🔍
-3% - +2%
-0.87ms - +0.60ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local21.64ms - 22.09ms-unsure 🔍
-0% - +3%
-0.02ms - +0.57ms
preact-main21.40ms - 21.79msunsure 🔍
-3% - +0%
-0.57ms - +0.02ms
-
text-update
  • Browser: chrome-headless
  • Sample size: 220
  • Built by: CI #5792
  • Commit: 528ae7c

duration

VersionAvg timevs preact-localvs preact-main
preact-local1.90ms - 2.01ms-unsure 🔍
-0% - +7%
-0.01ms - +0.13ms
preact-main1.85ms - 1.94msunsure 🔍
-7% - +0%
-0.13ms - +0.01ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.04ms - 1.09ms-unsure 🔍
-7% - +0%
-0.07ms - +0.00ms
preact-main1.07ms - 1.13msunsure 🔍
-0% - +7%
-0.00ms - +0.07ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local31.72ms - 32.27ms-unsure 🔍
-0% - +1%
-0.14ms - +0.48ms
preact-main31.70ms - 31.96msunsure 🔍
-1% - +0%
-0.48ms - +0.14ms
-

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-local32.13ms - 33.84ms-unsure 🔍
-4% - +4%
-1.23ms - +1.20ms
preact-main32.14ms - 33.87msunsure 🔍
-4% - +4%
-1.20ms - +1.23ms
-

usedJSHeapSize

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

tachometer-reporter-action v2 for CI

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown

Size Change: +17 B (+0.11%)

Total Size: 15.9 kB

📦 View Changed
Filename Size Change
compat/dist/compat.mjs 3.89 kB +11 B (+0.28%)
dist/preact.mjs 5.01 kB +6 B (+0.12%)
ℹ️ View Unchanged
Filename Size
debug/dist/debug.mjs 3.88 kB
devtools/dist/devtools.mjs 274 B
hooks/dist/hooks.mjs 1.5 kB
jsx-runtime/dist/jsxRuntime.mjs 861 B
test-utils/dist/testUtils.mjs 473 B

compressed-size-action

@JoviDeCroock
JoviDeCroock marked this pull request as ready for review August 6, 2026 13:49
@JoviDeCroock
JoviDeCroock merged commit d598771 into main Aug 6, 2026
13 checks passed
@JoviDeCroock
JoviDeCroock deleted the JoviDeCroock/issue-5109 branch August 6, 2026 15:12
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.

TypeError when SES lockdown is enabled

2 participants