Is better for performance to destructure dependency imports in create-react-app?

Viewed 46

Does it improve performance if I only import what I need? I think it depends on certain webpack config, is that true? if yes does create-react-app have that config integrated by default? How do I test how much performance I am gaining by doing this?

For example instead of this:

import React from "react";
import ReactDOM from "react-dom/client";

const root = ReactDOM.createRoot(document.getElementById("root") as HTMLElement);

root.render(
  <React.Fragment>
    <h1>Hello</h1>
  </React.Fragment>
);

I can use dependencies this way:

import { Fragment } from "react";
import { createRoot } from "react-dom/client";

const root = createRoot(document.getElementById("root") as HTMLElement);

root.render(
  <Fragment>
    <h1>Hello</h1>
  </Fragment>
);
1 Answers

You won't gain any performance by such imports.
Only thing you can reach by destructuring imports is reduce bundle size (read about tree-shaking).
But in case of import of react I afraid it won't make any effect, you can check size of builded budnle with and without destructuring

Related