Code Monkey home page Code Monkey logo

javascript-virtual-dom's Introduction

Reconciliation in React

Objectives

  1. Explain how React handles DOM updates in a performant manner

It Is Not a Virtual DOM

Earlier in the history of React, the term "Virtual DOM" was used to explain how React was able to perform better than the traditional DOM.

The term 'Virtual DOM' fails to really explain what is happening and may lead to a misunderstanding of what is happening behind the scenes when React renders.

Dan Abramov No Longer Likes Virtual DOM

In this lesson, we're going to briefly review how React handles updates to the screen. This process is known as Reconciliation

Updating the DOM

By now, you should already know what the DOM is: a programmatic representation of the document we see in the browser. In JavaScript applications, DOM elements can be added and changed with code. It's possible to build highly complex websites with hundreds or thousands of DOM elements using plain JavaScript. Maybe more importantly, through the DOM, JavaScript allows us to build highly interactive webpages that update dynamically without refreshing. This can come with some challenges, though.

When the DOM updates, the browser recalculates CSS, lays out the DOM tree and 'repaints' the display. This typically happens so fast you barely notice. However, on a highly interactive website, or on a website where the JavaScript is updating the DOM excessively, the process of recalculating and repainting the display can result in noticeably poor performance.

Any time you want your website or app to update without refreshing, you'll need to update the DOM; there is no avoiding it. However, React has some neat tricks for being smart about these updates.

Reconciliation, Briefly

In React, we know that we write components that return JSX elements. These JSX elements represent DOM elements, and when rendered, become those elements on a webpage.

During the initial render, React also uses these elements to build a 'tree' that represents what the DOM currently looks like, referred to as the current tree. When updates are made that would cause a re-render in React, a second tree, the workInProgress tree is created, representing what the DOM will look like. When all updates are processed, the workInProgress tree is used to update the DOM and the current tree is updated to reflect the new updates.

This is a key part of React's performance optimization - React uses these trees as an intermediate step between updates within components (like a change of state) and updates to the DOM. This helps in two ways:

Grouped Updates

Updates can be grouped together. By waiting until all updates are processed before committing the workInProgress tree to the DOM, excessive repaints are avoided.

Say, for instance, you have an app with many components, each colored a shade of blue, and a button, that when pressed, turns all those components to red. When that button is pressed, React will put together a tree containing all the components along with their updated properties, THEN commit all the changes to the DOM at once. This only requires one repaint. Without this design, we could end up with code that updates the DOM for each individual part of the app, one repaint for each part.

Diffing Changes

In addition to grouping updates to the DOM, React can apply a diffing algorithm to quickly see what specific pieces of DOM need to be updated and how. This reduces the number of DOM changes that need to be made and lets React be particular in its updates, improving performance.

In plain JavaScript some DOM changes are better than others in terms of performance. For example, say you want to add something inside a ul in your DOM. Using innerHTML will work:

ul.innerHTML += "<li>A final list item</li>";

But this rebuilds the entire DOM inside ul. On the other hand, using appendChild would not cause a rebuild:

let li = document.createElement("li");
li.textContent = "A final list item";
ul.appendChild(li);

React's diffing algorithm is designed to identify changes between what the current DOM looks like and what it will look like (the current and workInProgress trees). Based on the changes it identifies, different DOM updates will be performed to avoid rebuilding unnecessarily.

A more detailed explanation of the steps of this diffing process can be found in React's Reconciliation documentation

Conclusion

There are some misconceptions floating around regarding the DOM being slow, often related to how frameworks like React can improve performance. While DOM manipulation itself isn't 'slow,' repainting what is displayed in the browser can be.

React can be very smart about handling DOM updates, which improves performance. Primarily, it does this in two ways: grouping DOM updates to prevent excessive repaints and being selective about what specifically needs to update and how.

Read more a more in-depth dive on these concepts here.

Resources

javascript-virtual-dom's People

Contributors

alveem avatar annjohn avatar danielseehausen avatar ihollander avatar lizbur10 avatar lukeghenco avatar maxwellbenton avatar pletcher avatar thomastuts avatar walwoodr avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

javascript-virtual-dom's Issues

Lesson shows up as lab on Learn

This lesson shows up as a lab on Learn, which results in the wrong checkboxes appearing on the right hand side. This means that students are unable to properly complete this lab, and must skip it by clicking the gray "Next lesson" button.

This lesson should be classified as a README.

Affected tracks:
v-000

cc @aturkewi

Does this need to be a lab?

The content seems to only be a Readme and the only test is a default test that is passing. Is the point of the lab to install dependencies?

"Virtual Dom" terminology is a bit dated and a bit confusing

The React core team has moved away from using the term "Virtual Dom" and in fact no longer uses it in any documentation; students will see the term less and less in the wild and it's known to cause confusion.
https://twitter.com/dan_abramov/status/1066328666341294080
https://medium.com/react-in-depth/inside-fiber-in-depth-overview-of-the-new-reconciliation-algorithm-in-react-e1c04700ef6e

I'd recommend against kicking off a new unit with this terminology. Instead I'd propose a high level overview using terms like Reconciliation and/or Diffing.
ie "React maintains a big tree / object representing what the DOM "should" look like. It diffs this against what the DOM actually looks like. (The concept of a diff can be explained via GH pull requests) And from there efficiently reconciles the difference and updates the DOM so it's always up to date."

Add "is" to this sentence

Below it should say "is used":

Even though virtual DOM performs very well and used in several high-profile libraries and frameworks, it's important to remember that it's still just a really clever workaround to the DOM being slow.

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.