Skip to content

Conversation

@Rich-Harris
Copy link
Member

I have always hated this code. It's been a bug magnet in the past, and is very confusing and convoluted. The rationale for it was that it's better for performance/memory to hoist these functions, but it feels like a flimsy justification — if it matters enough for event handlers that we need to jump through these hoops, we should be doing it for all functions.

More to the point, it just doesn't appear to be true. In my local testing, I can't observe any performance difference whatsoever between this branch and main when it comes to initialization. It would be hard to construct a test that measured the cost of actually running the handlers but it doesn't matter because event handlers are, almost by definition, not hot code paths.

As for memory, my measurements indicate that the hoisted functions actually cost more than the non-hoisted ones. Arrays aren't free.

My understanding is that the performance benefits come from not having to register an event handler on each element (where even if you're using the same handler function on every element, a new object gets created each time), not from the deduplication. So we should just get rid of this.

Why now? I'm investigating some changes to the compiler that would enable out-of-order rendering (to get rid of more async waterfalls) and I just do not have the patience to work around this stuff.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

@changeset-bot
Copy link

changeset-bot bot commented Oct 24, 2025

⚠️ No Changeset found

Latest commit: 279796a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@svelte-docs-bot
Copy link

@github-actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@17030

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're creating so many lambda functions elsewhere that I also cannot imagine this having any impact - let's get rid of it

@Rich-Harris Rich-Harris merged commit 4eb432e into main Oct 24, 2025
18 checks passed
@Rich-Harris Rich-Harris deleted the remove-event-hoisting branch October 24, 2025 21:27
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.

3 participants