{"componentChunkName":"component---src-templates-blog-post-js","path":"/React/react-concept-1---virtual-dom/","webpackCompilationHash":"f92dbf25810602c1889e","result":{"data":{"site":{"siteMetadata":{"title":"Web Devlog","author":"RossenaHuh","siteUrl":"https://github.com/rossenahuh","comment":{"disqusShortName":"RossenaHuh","utterances":"rossenahuh/my-dev-blog.git"},"sponsor":{"buyMeACoffeeId":"rossena"}}},"markdownRemark":{"id":"f731876a-2795-5f9c-82bd-13bb2ca05a26","excerpt":"What is the Virtual DOM?VDOM is a programming concept where an virtual representation of a UI is kept in memory and synced with the real DOM by a library such as ReactDOM.This approach enables the declarative API of React detect the exact changes of every update: You tell React…","html":"<h2 id=\"what-is-the-virtual-dom\"><a href=\"#what-is-the-virtual-dom\" aria-label=\"what is the virtual dom permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is the Virtual DOM?</h2>\n<p>VDOM is a programming concept where an virtual representation of a UI is kept in memory and synced with the real DOM by a library such as ReactDOM.</p>\n<p>This approach enables the declarative API of React detect the exact changes of every update: You tell React what state you want the UI to be in, and it makes sure the DOM matches that state.</p>\n<h2 id=\"is-react-faster-than-dom\"><a href=\"#is-react-faster-than-dom\" aria-label=\"is react faster than dom permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Is React “faster than DOM”?</h2>\n<p>There are some myth out there regarding React and virtual DOM and Dan Abramov’s answer for above question was ‘NO’.</p>\n<p>He said people think there is something in virual DOM but the truth is it is <strong>unnecessarily</strong> updating or reading from DOM nodes in a bad order- normally reading should come first then writing. So it actually can be bad for performance.</p>\n<h2 id=\"what-really-happens-is\"><a href=\"#what-really-happens-is\" aria-label=\"what really happens is permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What really happens is:</h2>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">internalInstance.props = newElement.props</code></pre></div>\n<p>Nothing fancy, it’s just <strong>reassignment</strong>, keeping the props around. “Update” might be a too-much-term for this, he said. The more expensive part is calling React components recursively to learn which elements they render to, and <strong>diffing</strong> child lists.</p>\n<br>\n<h3 id=\"reconciliation--commit\"><a href=\"#reconciliation--commit\" aria-label=\"reconciliation  commit permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reconciliation &#x26; Commit</h3>\n<p>All of the above is <strong>reconciliation</strong>. Applying DOM changes, which called <strong>commit</strong>, is usually fast.</p>\n<p><strong>Reconciliation takes longer than commit.</strong> This is why React Team is making reconciliation pausable in Fiber(an ongoing reimplementation of React’s core algorithm whose one key feature is the ability to pause, abort, or reuse work as new updates come in).</p>\n<p>To recap, Virtual DOM doesn’t necessarily make React any faster, because it’s doing unnecessary works, but by letting a real DOM only update the changes, it reduces commit phase.</p>\n<br>\n<blockquote>\n<p>React helps create maintainalbe applications, and is <strong>fast enough</strong> for most use cases. <br><br> - Dan Abramov</p>\n</blockquote>\n<br>\n<br>\n<p><em>References</em></p>\n<p><a href=\"https://reactjs.org/docs/faq-internals.html\">https://reactjs.org/docs/faq-internals.html</a>\n<a href=\"https://github.com/acdlite/react-fiber-architecture\">https://github.com/acdlite/react-fiber-architecture</a>\n<a href=\"https://twitter.com/dan_abramov/status/842329893044146176\">https://twitter.com/dan_abramov/status/842329893044146176</a></p>\n<br>\n<br>","frontmatter":{"title":"React Concept 1 - Virtual DOM and the myths around it solved","date":"July 19, 2019"}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"/React/react-concept-1---virtual-dom/","previous":{"fields":{"slug":"/development/sql-vs-nosql--which-one-you-might-want-to-use/"},"frontmatter":{"title":"SQL vs NoSQL- Which one you might want to use","category":"Development"}},"next":null}}}