Skip to content

Optimize JSXSpreadAttribute JS emit when it contains ObjectLiteralExpression #49099

Closed

Description

Suggestion

🔍 Search Terms

jsx spread optimization babel

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (not for most of them)
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I've noticed that TypeScript emits a different JS output for some cases involving JSXSpreadAttribute that contains ObjectLiteralExpression than Babel. It seems that Babel has decided to optimize some of the situations and tries to avoid emitting unnecessary Object.assign calls/spread assignments.

// input
<Test {...{ foo: "test", bar: 42}} />;

// TS output
React.createElement(Test, Object.assign({}, { foo: "test", bar: 42 }));

// Babel output
React.createElement(Test, { foo: "test", bar: 42 });

So perhaps TS could follow the suite here and avoid this Object.assign call. This would come with minor bundlesize and perf gains.

Activity

  1. RyanCavanaugh commented on May 13, 2022

    @RyanCavanaugh
    Member

    Wesley Wigham (@weswigham) any concerns? Seems fine to me to match Babel

  2. weswigham commented on May 13, 2022

    @weswigham
    Member

    It's not a safe transform to make if a property in the object is named __proto__.

  3. Andarist commented on May 13, 2022

    @Andarist
    ContributorAuthor

    Sure, stuff like this can be detected in the algorithm and we might skip this optimization if there is __proto__ in the spread object. It seems though that Babel doesn't quite care about this and applies this optimization regardless of that (might be a bug in their code, maybe it's worth reporting there).

  4. weswigham commented on May 14, 2022

    @weswigham
    Member

    I'd definitely call it a bug in their code - I'm ok skipping the call when we statically know it's definitely safe, but there are definitely cases where it isn't.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: JSX/TSXRelates to the JSX parser and emitterDomain: PerformanceReports of unusually slow behaviorSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions