Skip to content

mdx-js/runtime: Avoid unstable element type to improve render performance #1655

Closed
@marcustyphoon

Description

@marcustyphoon

Heya! We're using the runtime package to live-render a preview window in netlify-cms-app. Naturally, the performance of this use case is never going to be amazing, but I believe I found a way to improve it noticeably.

Subject of the feature

mdx-js/runtime currently creates and calls a function that defines and returns an <MDXContent> component containing the desired content. I believe that, because this is a function that is defined on every render, its type is different on every render, so React's reconciler will always tear down the DOM component tree rather than diffing the DOM elements, if mdx-js/runtime is used live.

const suffix = `return React.createElement(
MDXProvider,
{components},
React.createElement(MDXContent, props)
)`

To test this, I made this change, and it seemed in a quick test to drop our render times by 4x. (From 4000ms... don't ask :P)

- React.createElement(MDXContent, props)
+ React.createElement(React.Fragment, null, MDXContent(props))

I'm anything but a React DOM expert or familiar with this codebase, so I imagine there's a much, much better way to do this.

Expected behavior

The MDX runtime creates the same component type tree when given the same input, as far as the React reconciler is concerned.

Alternatives

I can't think of any, besides "the performance is not quite as good as it could be."

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions