Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast JSX: Don't clone props object #28768

Merged
merged 1 commit into from
Apr 5, 2024
Merged

Fast JSX: Don't clone props object #28768

merged 1 commit into from
Apr 5, 2024

Commits on Apr 5, 2024

  1. Fast JSX: Don't clone props object

    (Unless "key" is spread onto the element.)
    
    Historically, the JSX runtime clones the props object that is passed in.
    We've done this for two reasons.
    
    One reason is that there are certain prop names that are reserved by
    React, like `key` and (before React 19) `ref`. These are not actual
    props and are not observable by the target component; React uses them
    internally but removes them from the props object before passing them
    to userspace.
    
    The second reason is that the classic JSX runtime, `createElement`, is
    both a compiler target _and_ a public API that can be called manually.
    Therefore, we can't assume that the props object that is passed into
    `createElement` won't be mutated by userspace code after it is
    passed in.
    
    However, the new JSX runtime, `jsx``, is not a public API — it's solely
    a compiler target, and the compiler _will_ always pass a fresh, inline
    object. So the only reason to clone the props is if a reserved prop name
    is used.
    
    In React 19, `ref` is no longer a reserved prop name, and `key` will
    only appear in the props object if it is spread onto the element.
    (Because if `key` is statically defined, the compiler will pass it as a
    separate argument to the `jsx` function.) So the only remaining reason
    to clone the props object is if `key` is spread onto the element, which
    is a rare case, and also triggers a warning in development.
    
    In a future release, we will not remove a spread key from the props
    object. (But we'll still warn.) We'll always pass the object
    straight through.
    
    The expected impact is much faster JSX element creation, which in many
    apps is a significant slice of the overall runtime cost of rendering.
    acdlite committed Apr 5, 2024
    Configuration menu
    Copy the full SHA
    970e6e7 View commit details
    Browse the repository at this point in the history