<?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: Adam Płócieniak</title>
    <description>The latest articles on DEV Community by Adam Płócieniak (@adasq).</description>
    <link>https://dev.to/adasq</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%2F629320%2F7a2bdd59-62dd-4cf6-ad11-41bda9ba24f8.png</url>
      <title>DEV Community: Adam Płócieniak</title>
      <link>https://dev.to/adasq</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adasq"/>
    <language>en</language>
    <item>
      <title>How Does React Work? Brief History - PART 1</title>
      <dc:creator>Adam Płócieniak</dc:creator>
      <pubDate>Thu, 12 Aug 2021 07:19:16 +0000</pubDate>
      <link>https://dev.to/hotjar/how-does-react-work-brief-history-part-1-1d12</link>
      <guid>https://dev.to/hotjar/how-does-react-work-brief-history-part-1-1d12</guid>
      <description>&lt;p&gt;&lt;em&gt;The article’s goal is to shed some light on the framework internals and historical insights standing behind the implementation decision made by the React Team at the time. I assume you are already aware of basic JavaScript / React and JSX concepts. So let’s kick off with some history first.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It’s&lt;/strong&gt; &lt;strong&gt;2013&lt;/strong&gt;. The React framework is born (version &lt;a href="https://github.com/facebook/react/blob/master/CHANGELOG.md#030-may-29-2013" rel="noopener noreferrer"&gt;0.3.0&lt;/a&gt; is rolled out to the public) and it’s immediately loved by the community. It stands out with its simplicity, one-way-data-binding, and - what’s obvious - declarative API powered by original, attractive to the community syntax extension, JSX.&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%2Fuploads%2Farticles%2F22vfqxaoii2y7eh2l978.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%2Fuploads%2Farticles%2F22vfqxaoii2y7eh2l978.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;React’s JSX representation of an example app&lt;/em&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;The following months and years bring new releases; bugs are fixed, features and improvements are added. The community grows, the tooling ecosystem supporting its development flourishes. &lt;a href="https://reactnative.dev/" rel="noopener noreferrer"&gt;React Native&lt;/a&gt; is embraced by mobile native developers, which brings even more popularity to the framework. But there is one thing around the framework that does not change at all. Its internal logic (so-called &lt;strong&gt;Reconciliation Algorithm&lt;/strong&gt;) is responsible for all of the “magic” - starting from when an application's state changes until direct DOM updates are performed.&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%2Fuploads%2Farticles%2Fsl95fqs7tlego1qjyhzp.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%2Fuploads%2Farticles%2Fsl95fqs7tlego1qjyhzp.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;Input and output of the Reconciliation algorithm&lt;/em&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Briefly, here’s how it works:&lt;/p&gt;

&lt;p&gt;(1) Every state change (e.g., clicking “Buy product” from the &lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u95tpve85arsv6ic2ulh.png" rel="noopener noreferrer"&gt;example application&lt;/a&gt;) forces building a so-called &lt;a href="https://reactjs.org/docs/faq-internals.html#what-is-the-virtual-dom" rel="noopener noreferrer"&gt;Virtual DOM&lt;/a&gt;, representing the current state of the application. It's a structure of components.&lt;/p&gt;

&lt;p&gt;(2) Then, the newly created Virtual DOM tree is compared with a previously generated Virtual DOM tree representing the application’s state already displayed to a user. Discrepancies between those Virtual DOM structures are simple information, what we should change, e.g.:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;update attribute X for A element,&lt;/li&gt;
&lt;li&gt;remove element B,&lt;/li&gt;
&lt;li&gt;or append element C as a child of element A.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(3) The information is utilized by separate modules - renderers (for browsers, it’s react-dom) which applies the minimal set of changes necessary to update UI (the DOM nodes for browsers). &lt;/p&gt;

&lt;p&gt;And that’s how React works, briefly.&lt;/p&gt;

&lt;p&gt;But how is the Virtual DOM built? If we had to write the function responsible for that on our own, it might look like:&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%2Fuploads%2Farticles%2Fu4pz63acgu3wik90ehko.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%2Fuploads%2Farticles%2Fu4pz63acgu3wik90ehko.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;High-Level Pseudocode of React’s Reconciliation Algorithm&lt;/em&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;The &lt;code&gt;render&lt;/code&gt; function drills down through the entire &lt;strong&gt;React Elements&lt;/strong&gt; structure (Virtual DOM) and &lt;strong&gt;reconciles&lt;/strong&gt; (or works on) every element.&lt;/p&gt;

&lt;p&gt;Let’s stop for a moment here. What is a React Element? A React Element is a simple building block for our application. It’s what we return from our components, like &lt;code&gt;&amp;lt;Loader /&amp;gt;&lt;/code&gt; in our &lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u95tpve85arsv6ic2ulh.png" rel="noopener noreferrer"&gt;example application&lt;/a&gt;. It’s worth mentioning that React Elements are not DOM Elements. Whenever I refer to an “element” below in the article, I mean React Element. If we talk about DOM elements, I will explicitly use the “DOM” to avoid misunderstanding. &lt;/p&gt;

&lt;p&gt;Back to the &lt;strong&gt;reconciliation&lt;/strong&gt;. Reconciling here means doing some element-related work. For component elements, it includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invoking the &lt;code&gt;render()&lt;/code&gt; method (for class-based components) or calling functional component’s function reference with given props,&lt;/li&gt;
&lt;li&gt;managing internal state,&lt;/li&gt;
&lt;li&gt;invoking Lifecycle Methods (e.g., &lt;code&gt;getDerrivedState&lt;/code&gt;),&lt;/li&gt;
&lt;li&gt;marking changes that need to be applied later on,&lt;/li&gt;
&lt;li&gt;and many more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By calling &lt;code&gt;render(&amp;lt;App /&amp;gt;)&lt;/code&gt; (I recommend getting familiar with our &lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u95tpve85arsv6ic2ulh.png" rel="noopener noreferrer"&gt;example application&lt;/a&gt; specified above, we will use this structure later on), we re-create the tree structure from top to bottom, using &lt;code&gt;render&lt;/code&gt; function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Starting from &lt;code&gt;render(App)&lt;/code&gt;, we work on the &lt;code&gt;App&lt;/code&gt; component,&lt;/li&gt;
&lt;li&gt;then we have some work to do with its child (&lt;code&gt;Content&lt;/code&gt;),&lt;/li&gt;
&lt;li&gt;then with its child’s children: &lt;code&gt;Loader&lt;/code&gt;, &lt;code&gt;Product&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;and so on, until we reach the last leaf of the structure of the elements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;It’s 2016&lt;/strong&gt;. Everyone is celebrating the framework's 3rd birthday (version &lt;a href="https://github.com/facebook/react/blob/master/CHANGELOG.md#1500-april-7-2016" rel="noopener noreferrer"&gt;15.0.0&lt;/a&gt; is released), but its parents (&lt;a href="https://reactjs.org/community/team.html" rel="noopener noreferrer"&gt;React Core Team&lt;/a&gt; with its lead, &lt;a href="https://reactjs.org/community/team.html#sebastian-markbage" rel="noopener noreferrer"&gt;Sebastian Markbåge&lt;/a&gt; - we will talk about him later in the article) are slightly worried about the future of the framework. But is there a reason to be concerned?&lt;/p&gt;

&lt;p&gt;It appears that the React has some “innate heart disease”, which limits its organic growth. Let’s have a brief look:&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%2Fuploads%2Farticles%2Fu4pz63acgu3wik90ehko.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%2Fuploads%2Farticles%2Fu4pz63acgu3wik90ehko.png" alt="image"&gt;&lt;/a&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%2Fuploads%2Farticles%2Fqh50bbazr5smdhhhob7c.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%2Fuploads%2Farticles%2Fqh50bbazr5smdhhhob7c.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;High-Level pseudocode of React’s core algorithm function vs. Fibonacci function&lt;/em&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;What’s common among &lt;code&gt;render()&lt;/code&gt; and &lt;code&gt;fib()&lt;/code&gt; functions?&lt;/p&gt;

&lt;p&gt;You’re right. It's a &lt;strong&gt;recursion&lt;/strong&gt;. The heart of the React framework relies strongly on recursion. But is it a problem at all?&lt;/p&gt;

&lt;p&gt;Web browsers are equipped with a single thread. We can do one operation at a time, so React operates in a limited environment. Although computing &lt;code&gt;fib(4)&lt;/code&gt; is not a challenge at all, computing &lt;code&gt;fib(4000)&lt;/code&gt; definitely is. Same for the Reconciliation Algorithm - building a Virtual DOM based on a thousand elements is a real challenge. Synchronous code execution blocks the main thread, so JavaScript’s event loop has to wait until the end of execution. During that time, none of the following activities can be performed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User input (e.g., handling user click event callback)&lt;/li&gt;
&lt;li&gt;Animations, layout calculations, repaints&lt;/li&gt;
&lt;li&gt;Handle incoming data (HTTP, WebSocket)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is a brilliant talk about event loop &lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ" rel="noopener noreferrer"&gt;here&lt;/a&gt;, so if you are not familiar with it or need a refresher, it’s definitely worth watching.&lt;/p&gt;

&lt;p&gt;Let’s talk now about the JS Stack; how does it look for both the &lt;code&gt;fib&lt;/code&gt; and &lt;code&gt;render&lt;/code&gt; functions?&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%2Fuploads%2Farticles%2Flnzfdehu9062vzyhc7rf.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%2Fuploads%2Farticles%2Flnzfdehu9062vzyhc7rf.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;Fibonacci function stack frames vs. Render function stack frames&lt;/em&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;The JS Stack grows as we move deeper in the structure, so the process simply can’t be paused because there is no straightforward way to do that in a recursion. &lt;strong&gt;We reconcile all of the elements in one shot or none at all&lt;/strong&gt;. What’s more, React's computation output is pushed onto the JS stack, so it's ditched immediately after the &lt;code&gt;render&lt;/code&gt; function returns. There is no way to reuse this work later on if it’s needed.&lt;/p&gt;

&lt;p&gt;Imagine a case of a heavy application with a massive number of components. We are in the middle of the Reconciliation Algorithm, and a user clicks a button. Such action is critical from a UX standpoint and should be handled immediately. But what happens?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clicking dispatches a DOM event. &lt;/li&gt;
&lt;li&gt;The event callback lands in a queue and waits (until the JS Stack is empty) to be processed.&lt;/li&gt;
&lt;li&gt;But the JS stack is “overwhelmed” by heavy React-related work, so the event callback waits…, waits, and waits for its turn until the Reconciliation Algorithm is done.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/QBd2kLB5qDmysEXre9/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/QBd2kLB5qDmysEXre9/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;(&lt;a href="https://i.giphy.com/media/QBd2kLB5qDmysEXre9/giphy.gif" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/em&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;There is an excellent &lt;a href="https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle" rel="noopener noreferrer"&gt;Sierpinski triangle&lt;/a&gt; example &lt;a href="https://github.com/adasq/react-fiber-vs-stack-demo" rel="noopener noreferrer"&gt;application&lt;/a&gt; on Github. It is a more tangible showcase of the problem. Keep in mind that it’s 2016, so the application is built on top of React 15.x. Here is how the application looks like:&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%2Fuploads%2Farticles%2Faw9vj90yygensayk763p.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%2Fuploads%2Farticles%2Faw9vj90yygensayk763p.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;Sierpinski triangle example application view&lt;/em&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;Each dot is a component displaying a number. Among state updates, there are a lot of other computations, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;animations (layout computations, painting), &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/claudiopro/react-fiber-vs-stack-demo/blob/master/stack.html#L93-L99" rel="noopener noreferrer"&gt;deliberate delays&lt;/a&gt;,&lt;/li&gt;
&lt;li&gt;a whole bunch of artificial and pointless &lt;a href="https://github.com/claudiopro/react-fiber-vs-stack-demo/blob/master/stack.html#L134-L139" rel="noopener noreferrer"&gt;state changes&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these simulate a heavy application. And &lt;a href="https://claudiopro.github.io/react-fiber-vs-stack-demo/stack.html" rel="noopener noreferrer"&gt;here&lt;/a&gt; is the application. Pretty sluggish, huh? Here’s how the top of the JS Stack looks like (I recommend watching this &lt;a href="https://youtu.be/Jb4X4hyaUCs" rel="noopener noreferrer"&gt;short video&lt;/a&gt;).&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%2Fuploads%2Farticles%2Fwijrareaznmkbphhtapp.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%2Fuploads%2Farticles%2Fwijrareaznmkbphhtapp.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;center&gt;&lt;em&gt;React 15.x JS Stack&lt;/em&gt;&lt;/center&gt;
&lt;img src="https://i.giphy.com/media/KSJ8UkuKHLmvK/giphy.gif"&gt;&lt;center&gt;&lt;em&gt;(&lt;a href="https://i.giphy.com/media/KSJ8UkuKHLmvK/giphy.gif" rel="noopener noreferrer"&gt;source&lt;/a&gt;)&lt;/em&gt;&lt;/center&gt;
&lt;br&gt;

&lt;p&gt;A synchronous and time-consuming function (in the Sierpinski’s triangle example, for my equipment, each “Task” takes ~300ms) reconciles the entire application from the top to the bottom of the elements tree, no matter what. &lt;/p&gt;

&lt;p&gt;The framework here is relentless. It overwhelms the main thread, which can’t perform any other types of work (animations, user’s input). It introduces a significant impact on the browser’s performance. It’s hard to build on the top of such architecture, isn’t it?&lt;/p&gt;

&lt;p&gt;And this is a huge limitation for the React Team. &lt;/p&gt;

&lt;p&gt;Of course, this is an example application built to show the problem. But we can quickly observe such pitfalls when the number of components grows in our real-world applications. Long and heavy lists are a perfect example here. It’s also why the helper libraries, like &lt;a href="https://github.com/bvaughn/react-virtualized" rel="noopener noreferrer"&gt;react-virtualized&lt;/a&gt; (or its lightweight version, &lt;a href="https://github.com/bvaughn/react-window" rel="noopener noreferrer"&gt;react-window&lt;/a&gt;), emerged and gained noticeable popularity by supporting the framework. Those libraries are &lt;a href="https://reactjs.org/docs/optimizing-performance.html#virtualize-long-lists" rel="noopener noreferrer"&gt;officially recommended&lt;/a&gt; on React docs pages.&lt;/p&gt;

&lt;p&gt;It’s also worth noticing that React is clever enough, and implements caching, exposes the keys API, introduces some trade-offs to reinforce performance which is a real deal, but still - it’s not enough to move forward (If you are interested in in-depth details of the algorithm, visit official docs &lt;a href="https://reactjs.org/docs/implementation-notes.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In the next part, I will explain what approach the React Team took to address those limitations.&lt;/p&gt;

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