<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: GaelFerrand</title>
    <description>The latest articles on DEV Community by GaelFerrand (@gaelferrand).</description>
    <link>https://dev.to/gaelferrand</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F497273%2Fd029ba81-5658-443d-b2f7-2ffcb763e5fc.png</url>
      <title>DEV Community: GaelFerrand</title>
      <link>https://dev.to/gaelferrand</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gaelferrand"/>
    <language>en</language>
    <item>
      <title>Why you should NOT use Material-UI</title>
      <dc:creator>GaelFerrand</dc:creator>
      <pubDate>Tue, 16 Feb 2021 17:32:17 +0000</pubDate>
      <link>https://dev.to/gaelferrand/why-you-should-not-use-material-ui-21nn</link>
      <guid>https://dev.to/gaelferrand/why-you-should-not-use-material-ui-21nn</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fddpw3xdoy629sak43uc7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fddpw3xdoy629sak43uc7.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://material-ui.com/" rel="noopener noreferrer"&gt;Material-UI&lt;/a&gt; is a popular components library (a competitor of &lt;a href="https://reactstrap.github.io/" rel="noopener noreferrer"&gt;Reactstrap&lt;/a&gt; that &lt;em&gt;unifies&lt;/em&gt; React and &lt;a href="https://material.io/" rel="noopener noreferrer"&gt;Material Design&lt;/a&gt;, the design system built by Google.&lt;/p&gt;

&lt;p&gt;As a React developer I've used Material-UI in countless projets and I must say that it's a great library that comes with its grid system, lots of components, and helps build consistent UIs quite fast.&lt;/p&gt;

&lt;p&gt;I was pretty happy with it until I used it for my personnal open-source project &lt;a href="https://github.com/OthrysDev/ammo" rel="noopener noreferrer"&gt;Ammo&lt;/a&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Quick parenthesis about Ammo: it's an open source projects which aims at capturing http requests and turn them into stress test engines scripts (such as Gatling). Not the focus of this article though, I won't dive into more details.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Context
&lt;/h1&gt;

&lt;p&gt;Before exposing my unhappiness with Material-UI, let's get a look at my struggle.&lt;/p&gt;

&lt;p&gt;In Ammo, I have a list of items that looks like the following : &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa9yeibbzs93u8dmkdvwi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa9yeibbzs93u8dmkdvwi.png" title="Ammo's list of items" alt="Ammo's list of items"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each item in the list can be collapsed / uncollapsed :&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpxif498nwcyctfjc0761.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpxif498nwcyctfjc0761.png" title="An uncollapsed item" alt="Uncollapsed item"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see here, when uncollapsed an item reveals a few things, but nothing too fancy nor complicated. On the left, we can oberve basic HTML divs with text, and on the right we have code snippets (formatted / prettified thanks to &lt;a href="https://www.npmjs.com/package/react-syntax-highlighter" rel="noopener noreferrer"&gt;React syntax highlighter&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yet, I would very quickly notice that the overall performance of the application is absolutely terrible.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What?&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  A first set of measures
&lt;/h1&gt;

&lt;p&gt;After developping the code-snippets-highlighting feature, just for the sake of being 100% happy with the feature, I tried to throw a couple items in my list and confirm that the syntax highlighting library did not tear down the performance of the app.&lt;/p&gt;

&lt;p&gt;The idea proved to be relevant as the performance turned out to be horrible.&lt;/p&gt;

&lt;p&gt;Just to illustrate how bad it was, here is a gif of what happend when adding 50 items to the interface (using a setInterval, adding 1 item every 300ms) : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9dpgy09kod7f7ss6ua2q.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9dpgy09kod7f7ss6ua2q.gif" title="The gif is not lagging : the app is" alt="ammo_lag"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the application is lagging like hell and a real pain to use. &lt;em&gt;With just 50 items !&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Coming from a gaming background, where one has to display images, animations, sound effects &amp;amp; musics, multiple layers of background etc. at 60FPS, seeing such a lag just for rendering HTML divs was too much to bear. So I dove into profiling.&lt;/p&gt;

&lt;h1&gt;
  
  
  Witch hunt: React edition
&lt;/h1&gt;

&lt;p&gt;My first intuition was that there was something wrong with React. After all, I had seen (and done, let's be honest) in the past lots of apps with unnecessary renders and performance-wise poor practices. So the first thing I made was to make sure that the list was optimized, doing 2 things : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Giving non-index, unique keys to each item in the list&lt;/li&gt;
&lt;li&gt;Memoize the already-rendered items so that they would not re-render when adding a new one. I used &lt;code&gt;memo&lt;/code&gt; but &lt;code&gt;useMemo&lt;/code&gt; is just as valid.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After that I profiled the app : &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvgon32zggadshv9yk7ml.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvgon32zggadshv9yk7ml.png" title="Profiling of 50 items" alt="First profiling"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can notice : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First of all, the memoization seems to be working very fine. We can see that the items already rendered are greyed out, meaning that they did not re-render&lt;/li&gt;
&lt;li&gt;We can see that the newly introduced item is indeed taking some render time&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;But what got me worried is the graph in the upper-right corner&lt;/strong&gt;. As you can see, renders are getting slower over time. The rendering of the new item starts off taking around 100ms, but when the list gets longer, it takes up to 500ms. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;What ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First of all, why is the number of items in the list having any influence at all on the rendering time of the new item? And then, 500ms to render some basic divs! Horse crap!&lt;/p&gt;

&lt;p&gt;Let's zoom in on the profiling of the rendering of one item : &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsz05us2q157336kvbunw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsz05us2q157336kvbunw.png" title="Profiling one item" alt="Profiling one item"&gt;&lt;/a&gt;&lt;br&gt;
On the image I highlighted 2 things : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On the right, we can see that the react-syntax-highlighter, my first source of worry, is not at all responsible for the poor performance. It renders decently fast&lt;/li&gt;
&lt;li&gt;What seems to be taking quite some time are the "headers" (on the left). &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Just to be clear, this is a "header" : &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F980ohrv3swgs7jxqpmra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F980ohrv3swgs7jxqpmra.png" title="A header" alt="Header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's really nothing, just two inlined-texts! How come it be that slow??? Just to prove my point even further, here is the code of the &lt;code&gt;&amp;lt;Header&amp;gt;&lt;/code&gt; component:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Box&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Typography&lt;/span&gt; &lt;span class="nx"&gt;variant&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;subtitle2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&amp;gt; &lt;/span&gt;&lt;span class="err"&gt;:
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headerValue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{value}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Typography&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Box&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;There is litteraly nothing fancy here. No hidden performance caveheat, it's just a few basic divs!&lt;/p&gt;

&lt;h1&gt;
  
  
  It all comes down to Material-UI
&lt;/h1&gt;

&lt;p&gt;Quite desperate, I tried lots of things, went through lots of forums, still trying to figure out how React could mess things this badly. And then, out of ideas, I naively tried to replace &lt;code&gt;&amp;lt;Box&amp;gt;&lt;/code&gt; components with &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Typography&lt;/span&gt; &lt;span class="nx"&gt;variant&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;subtitle2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&amp;gt; &lt;/span&gt;&lt;span class="err"&gt;:
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headerValue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{value}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Typography&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Stunningly, I saw some improvement in the performance!&lt;/strong&gt; I went a bit further and tried to get rid of as much Material-UI components as possible (mostly &lt;code&gt;&amp;lt;Box&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Typography&amp;gt;&lt;/code&gt;) and I ended up with this :&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq9klghh0w416tg5f3d3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq9klghh0w416tg5f3d3e.png" alt="new_profiling"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your read that right you'd see that : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This test is done with 500 items (~1000 renders). And it's fluid!&lt;/li&gt;
&lt;li&gt;The highest rendering peak is at ~110ms (instead of 500ms for 50 items)&lt;/li&gt;
&lt;li&gt;The items rendering times are consistent and do not increase with the number of items in the list&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F727fwlt3qfxg34z10q2g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F727fwlt3qfxg34z10q2g.gif" alt="ammo_fluid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So that's a big win! Now the app is decently fluid with 10 times more items! And its performance is consistent!&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Material-UI is a great library and has had my back in the past years, yet now I would strongly advise against using it if your application might present performance challenges. A simple form or a standard web page shouldn't be a problem, but still, keep that in mind.&lt;/p&gt;

&lt;p&gt;Of course I'm not the first one to find out about those problems, there are several issues open on Github (&lt;a href="https://github.com/mui-org/material-ui/issues/21657" rel="noopener noreferrer"&gt;this one for example&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Also, after all this optimization, you could still argue that a 110ms rendering time for one item is still huge, and I'd agree, but I'll leave things here for the time being. Any further optimization shall be a good topic for a new article!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
