<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Doctolib Nerds on Medium]]></title>
        <description><![CDATA[Stories by Doctolib Nerds on Medium]]></description>
        <link>https://medium.com/@doctolibnerds?source=rss-32253b641d68------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*U89eLtbD8o4xO9wa_irLHg.png</url>
            <title>Stories by Doctolib Nerds on Medium</title>
            <link>https://medium.com/@doctolibnerds?source=rss-32253b641d68------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 21:39:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@doctolibnerds/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Improve React performance with Babel]]></title>
            <link>https://medium.com/doctolib/improve-react-performance-with-babel-16f1becfaa25?source=rss-32253b641d68------2</link>
            <guid isPermaLink="false">https://medium.com/p/16f1becfaa25</guid>
            <category><![CDATA[babel]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Doctolib Nerds]]></dc:creator>
            <pubDate>Wed, 06 Apr 2016 11:42:06 GMT</pubDate>
            <atom:updated>2018-10-26T11:27:46.411Z</atom:updated>
            <content:encoded><![CDATA[<p>by <a href="https://medium.com/@oliviertassinari">Olivier Tassinari</a></p><p>React is well known for its <strong>virtual DOM</strong> implementation and its <a href="https://auth0.com/blog/2016/01/07/more-benchmarks-virtual-dom-vs-angular-12-vs-mithril-js-vs-the-rest/"><strong>good performance</strong></a> out of the box.</p><p>There are <a href="http://jaero.space/blog/react-performance-1">many ways</a> to speed it up. Just to name a few:</p><ul><li>You can use <em>process.env.node_env = ‘production’</em> to disable all the checks that React is doing in the development environment. For instance, that’s going to bypass the <em>propTypes</em> validation.</li><li>You can prune the reconciliation tree with the <em>shouldComponentUpdate</em> lifecycle method. The depth of the pruned node matters a lot. The lower the depth, the better.</li><li>You can smartly use the <em>key</em> property on a long list of elements. The idea is to give useful information to React so it can identify each element and perform as few DOM mutations as possible.</li></ul><p>These three approaches are very efficient. You will sooner or later have to use them as your application grows and you want to give users the best possible experience. Sometimes, <strong>these speedup tips are</strong> <strong>not enough</strong> and you start to investigate ways to push performance further.</p><h3>Babel, a powerful tool</h3><p>You may already know Babel. It has gotten famous by allowing us to <strong>use ES6</strong> in production with <a href="https://kangax.github.io/compat-table/es6/">browsers</a> that are not supporting it yet.</p><p>It used to be called <em>6to5</em>, but with version 6, Babel has become much more than that. It’s now a powerful tool to apply code transforms at the <a href="https://youtu.be/OZGgVxFxSIs">AST level</a>. You can do such things with other tools but this one is pretty convenient. People may not realize it, but they are already using a lot of AST transformation functions when they write ES6 and JSX code. Those ES6 and JSX transformation functions are packaged under two presets:</p><ul><li><a href="https://babeljs.io/docs/plugins/preset-es2015/">babel-preset-es2015</a></li><li><a href="https://babeljs.io/docs/plugins/preset-react/">babel-preset-react</a></li></ul><p>Things started to be really interesting when Facebook released React <em>0.14.0</em>. They have introduced <strong>two compiler optimizations</strong> that we can enable in production. We hadn’t heard anything about it on Twitter nor Medium. We have realized it by carefully reading the <a href="https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html#compiler-optimizations">release note</a>:</p><blockquote><em>React now supports two compiler optimizations that can be enabled in Babel 5.8.24 and newer. Both of these transforms should be </em><strong><em>enabled only in production</em></strong><em> (e.g., just before minifying your code) because although they improve runtime performance, they make warning messages more cryptic and skip important checks that happen in development mode, including propTypes.</em></blockquote><h3>Constant hoisting for React elements</h3><p>The first compiler optimization proposed by Facebook hoists the creation of elements that are <strong>fully static</strong> to the top level. A component is fully static or <a href="https://en.wikipedia.org/wiki/Referential_transparency">referentially transparent</a> when it can be replaced with its value without changing the behavior.</p><p>It’s a Babel plugin called <a href="https://babeljs.io/docs/plugins/transform-react-constant-elements/">transform-react-constant-elements</a>. Let’s have a look at how it behaves with a simple example:</p><p><strong>In</strong></p><pre>const Hr = () =&gt; {<br>  return &lt;hr className=&quot;hr&quot; /&gt;;<br>}</pre><p><strong>Out</strong></p><pre>const _ref = &lt;hr className=&quot;hr&quot; /&gt;;</pre><pre>const Hr = () =&gt; {<br>  return _ref;<br>};</pre><p>This transform has <strong>two advantages</strong>:</p><ul><li>It tells React that the subtree hasn’t changed so React can completely skip it when reconciling.</li><li>It reduces calls to <em>React.createElement</em> and the resulting memory allocations.</li></ul><p>However, there are some <strong>limitations</strong> that you should be aware of. It has two <a href="https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-constant-elements/test/fixtures/constant-elements">documented deoptimizations</a>. It won’t work if you are using the <em>ref</em> property or if you are spreading properties.</p><h3>Inlining React elements</h3><p>The second compiler optimization proposed by Facebook is replacing the <a href="https://github.com/facebook/react/blob/14b19870fe0504cbfb43127983f9aee411334cd6/src/isomorphic/classic/element/ReactElement.js#L117"><em>React.createElement</em></a> function with a more optimized one for production: <a href="https://github.com/babel/babel/blob/8fb6f878a3843c4cc7dbe20168e633b1551ba699/packages/babel-helpers/src/helpers.js#L14"><em>babelHelpers.jsx</em></a>.</p><p>It’s a Babel plugin called <a href="https://babeljs.io/docs/plugins/transform-react-inline-elements/">transform-react-inline-elements</a>. Let’s have a look at how it behaves with a simple example:</p><p><strong>In</strong></p><pre>&lt;Baz foo=&quot;bar&quot; key=&quot;1&quot; /&gt;;</pre><p><strong>Out</strong></p><pre>babelHelpers.jsx(Baz, {<br>  foo: &#39;bar&#39;<br>}, &#39;1&#39;);</pre><pre>/**<br> * Instead of<br> *<br> * React.createElement(Baz, {<br> *   foo: &#39;bar&#39;,<br> *   key: &#39;1&#39;,<br> * });<br> */</pre><p>The advantage of this transform is <strong>skipping a loop through props</strong>. The <em>babelHelpers.jsx</em> method has a slightly different API than the <em>React.createElement</em>. It accepts a props argument that skips this <a href="https://github.com/facebook/react/blob/14b19870fe0504cbfb43127983f9aee411334cd6/src/isomorphic/classic/element/ReactElement.js#L140-L146">specific loop</a>:</p><pre>// Remaining properties are added to a new props object<br>for (propName in config) {<br>  if (config.hasOwnProperty(propName) &amp;&amp;<br>  !RESERVED_PROPS.hasOwnProperty(propName)) {<br>    props[propName] = config[propName];<br>  }<br>}</pre><p>However, there are some <strong>limitations</strong> that you should be aware of. It has two <a href="https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-constant-elements/test/fixtures/constant-elements">documented deoptimizations</a>, the same than before. It won’t work if you are using the <em>ref</em> property or if you are spreading properties.<br>If you are targeting older browser and not parsing the <em>node_modules </em>with Babel, <a href="https://phabricator.babeljs.io/T2517">you need</a> a global polyfill for ES6 <em>Symbol.</em></p><p>I have also noticed <a href="https://github.com/erikras/react-redux-universal-hot-example/pull/1037#discussion-diff-56849536">one issue</a>. You can’t use the JSX syntax like this:</p><pre>&lt;Navbar.Header /<em>&gt;</em></pre><h3>Remove propTypes</h3><p>Looking at the power of the previous transforms, we have soon been wondering if we couldn’t get rid of <em>propTypes</em> in production. As soon as we set <em>process.env.node_env = ‘production’</em> all <em>propTypes</em> definitions are simply <strong>dead code</strong>.</p><p>I had found a great plugin written by <em>Nikita Gusakov</em> for this specific use case. Unfortunately, at that time, it was only working with Babel 5. I ended up forking it with <a href="https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types">transform-react-remove-prop-types</a>. Let’s have a look at how it behaves with a simple example:</p><p><strong>In</strong></p><pre>const Baz = () =&gt; (<br>  &lt;div /&gt;<br>);</pre><pre>Baz.propTypes = {<br>  foo: React.PropTypes.string<br>};</pre><p><strong>Out</strong></p><pre>const Baz = () =&gt; (<br>  &lt;div /&gt;<br>);</pre><p>The advantage of this transform is saving bandwidth. We are not aware of any limitation.</p><h3>Let’s benchmark this!</h3><p>Applying plugins in production without ways to measure their impact is like shooting in the dark. You shouldn’t do it.</p><p>How efficient are those optimizations? At Doctolib, we were wondering the same thing. We have built a <strong>complex calendar</strong> that can display thousands of appointments with doctors on a single view. We want the interface to be as fast as possible so that doctors can focus on what matters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*Zl4AD1-ULCuyecUa.png" /><figcaption>Anonymized calendar</figcaption></figure><p>Thankfully, some tools are available.</p><h3>react-addons-perf</h3><p>The first tool that we can use to benchmark is an addon built by Facebook: <a href="https://facebook.github.io/react/docs/perf.html">react-addons-perf</a>. It’s pretty <strong>convenient to measure wasted time</strong> by component. In our case, we are going to focus on the inclusive time. The protocol is the following. We are rendering 50 times the calendar component with a fair amount of events (500). In the meantime, <em>react-addons-perf</em> is measuring the inclusive time spent. Here are the results (the lower the better):</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*Ubwo_egfBr5uavMJ.png" /></figure><p>Notice that we can significantly improve the runtime performance with the three previous transforms. The runtime <strong>win is around 25%</strong> without significant build size impact.</p><p>However, we <strong>can’t rely on this benchmark</strong> as the addon requires <em>process.env.node_env != ‘production’</em> to work. We need a way to benchmark the rendering with <em>process.env.node_env = ‘production’</em>.</p><h3>benchmark.js</h3><p>The second tool that we have used is React agnostic: <a href="https://benchmarkjs.com/">benchmark.js</a>. The protocol is pretty much the same. We are mounting and unmounting the calendar component 100 times. Here are the results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*YdfSxYJ8_Xk3b3e-.png" /></figure><p>Notice that the very first step benchmarked is setting <em>process.env.node_env = ‘production’ </em>and that the runtime unit changed. We are measuring operation per second, the higher, the better.<br>The runtime <strong>win is 7.24% ± 0.45% </strong>with those three previous transforms. That’s less impressive but still interesting as enabling these transforms is <strong>cheap</strong>.</p><p>You can check out <a href="https://github.com/oliviertassinari/react-benchmark/">the code</a> for this benchmarks.</p><h3>To wrap up</h3><p>We highly advise you to consider adding those different compiler optimizations. We have already started spreading the word to the community: <a href="https://github.com/FormidableLabs/spectacle/pull/154">spectacle</a>, <a href="https://github.com/davezuko/react-redux-starter-kit/pull/561">react-redux-starter-kit</a>, <a href="https://github.com/kriasoft/react-starter-kit/pull/440">react-starter-kit</a>, <a href="https://github.com/erikras/react-redux-universal-hot-example/pull/1037">react-redux-universal-hot-example</a>.</p><p>But keep in mind that these transforms should <strong>only be enabled in production</strong>. They make warning messages more cryptic and skip important checks that happen in development mode, like <em>propTypes</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16f1becfaa25" width="1" height="1" alt=""><hr><p><a href="https://medium.com/doctolib/improve-react-performance-with-babel-16f1becfaa25">Improve React performance with Babel</a> was originally published in <a href="https://medium.com/doctolib">Doctolib</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>