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

Optimize bundle size (now 317KB with v4) #24381

Closed
masbaehr opened this issue Jan 12, 2021 · 9 comments
Closed

Optimize bundle size (now 317KB with v4) #24381

masbaehr opened this issue Jan 12, 2021 · 9 comments
Labels
performance support: question Community support but can be turned into an improvement

Comments

@masbaehr
Copy link

Hi, this is not an issue but i'd like to know if there is some room to improve the bundle size, or if there is something i can do as a user to reduce the size. currently it is like this:

https://bundlephobia.com/result?p=@material-ui/core@4.11.2

There seem to be some components which are rather big in comparision for example TablePagination SwipeableDrawer TabScrollButton

I was unable to check for the v5 though yet. Will it improve?

@masbaehr masbaehr added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 12, 2021
@oliviertassinari oliviertassinari added performance support: question Community support but can be turned into an improvement and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 12, 2021
@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 12, 2021

@masbaehr You can track the bundle size of v5 using https://bundlephobia.com/result?p=@material-ui/core@next. We have made significant progress in v5 compared to v4:

  • split IE11 into a different bundle -6kB
  • migrate to emotion -5kB
  • Popper.js v2 upgrade -700B

We still have a few opportunities to improve bundle size with better tree-shaking between components:

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 12, 2021

⚠️ the total cost is almost useless, nobody should need to import all the components in a single page.

Capture d’écran 2021-01-12 à 11 31 31

Instead, consider the cost of individual modules but consider that when importing two modules, the bundle size isn't the addition of the two reported sizes, there is a lot of shared modules.

Capture d’écran 2021-01-12 à 11 31 34

@masbaehr
Copy link
Author

In a single page - this is true. But isn't everything still packed into one *.chunk.js file? That is at least the case if you're not using code-splitting. I already do this for heavy stuff like recharts, but material-ui being something which is needed almost everywhere I find it rather hard to do this. Throughout all the pages, I use most of the components somewhere. While 381k 111k gzipped is not much per se, the thing is, it adds up rather quickly.

@eps1lon
Copy link
Member

eps1lon commented Jan 12, 2021

While 381k 111k gzipped is not much per se, the thing is, it adds up rather quickly.

With what does it add up though? Most of the components implement base UI patterns on the web. We don't recommend that you import 3 different Autocomplete implementations in your page. So it shouldn't ever add up in your bundle.

I already do this for heavy stuff like recharts, but material-ui being something which is needed almost everywhere I find it rather hard to do this.

What makes this easier to do with recharts but not Material-UI?

Again, are you importing all of material-ui everywhere?

It would be more helpful to share the bundle you're currently shipping. That helps us finding duplicates we can potentially eliminate. For example, maybe most of our users need a different popper library in which case we should use that instead of @popperjs/core. Or maybe there are other Autocomplete implementations that are preferred over our solution. A blanket "reduce the bundle size" is not actionable for us since we always try to reduce our footprint on the final bundle. It falls under the same category as "make components more accessible" or "make components faster" or "add more components".

@masbaehr
Copy link
Author

I have some heavier dependencies like jodit editor (600KB) or the already mentioned recharts (645KB) - which i have now externalized using lazy loading patterns. I never have imported all of material ui in the pages only some parts of it.

So i just noticed material-ui being a large portion of it. But i understand your point i'm not ranting i just wanted to ask if there is some room for improvement. Maybe even improvement i can personally apply for material-ui.

E.g if i never import the yellowish marked components, will they be removed from the bundle?

I know there are some other dependencies which can probably be replaced. This is what i will do next, going trough them one-by-one
image

@eps1lon
Copy link
Member

eps1lon commented Jan 12, 2021

I never have imported all of material ui in the pages only some parts of it.

From the the bundle screenshot you posted it looks like you do import all of Material-UI.

Other notes: material-ui-phone-number being 100kB big is suspicious. They have a dependency on lodash and are published as commonJS so I do suspect they're bundling all of lodash.

@masbaehr
Copy link
Author

This is how i usually do the imports where i need something in material-ui
image

Curious fact: I never imported SwipeableDrawer , but it is being built into the file. Any reason why this is? (I use Drawer though)

@eps1lon
Copy link
Member

eps1lon commented Jan 12, 2021

Curious fact: I never imported SwipeableDrawer , but it is being built into the file. Any reason why this is? (I use Drawer though)

Something in your app is responsible for importing all of Material-UI it seems.

@masbaehr
Copy link
Author

material-table-core/core#45 Maybe it's material-table triggering it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

3 participants