Home > Web Front-end > JS Tutorial > Why Virtual DOM: Render and Performance

Why Virtual DOM: Render and Performance

Linda Hamilton
Release: 2024-11-03 14:11:30
Original
781 people have browsed it

In this article, we will examine the dom in detail and what the virtual dom is.

What is the DOM (Document Objec Model) ?

When a web page is loaded into the browser, the browser generates HTML to display the requested page to . This html structure is like a tree. DOM is formed as a result of converting the content of a web document (HTML or XML) into an object-oriented structure by the browser.Thanks to this structure, the page content is organized in a tree structure in the browser, and each HTML tag or content is represented as a "node".


Now explore the document object model :

Let's write an example code to access the document object.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document object model</title>
</head>
<body>
    <div class="card" style="width: 18rem;">
        <img class="card-img-top"  alt="Card image cap">
        <div class="card-body">
          <h5 class="card-title">Lorem, ipsum dolor.</h5>
          <p class="card-text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Hic, tenetur!</p>
          <a href="#" class="btn btn-primary">Lorem, ipsum.</a>
        </div>
      </div>


    <script src="/script.js"></script>
</body>
</html>
Copy after login
console.log(document)
Copy after login

Output :

Why Virtual DOM: Render and Performance


We can access the dom with console.log(document). When we examine the Document object, we understand that this object represents an HTML document. In other words, the document object contains all the tags in an HTML document.

With Javascript, we can access the tags inside the document object and manipulate the tags inside this document object to create dynamic web pages. Let's give an example of how to access :

const wrapper = document.getElementById("card")

console.log(wrapper)
Copy after login

Output:

Why Virtual DOM: Render and Performance


What is the Virtual DOM?

Why Virtual DOM: Render and Performance

We now know that the easiest way to change the dom ("Change HTML") is to change the innerHTML property in an element. This method of modifying html does not performance well in DOM repainting (" Updating what the user sees ").This is because innerHTML needs to parse DOM nodes from a string, preprocess, and append it. if there are too many html mutations on a web page, there will be a performance issue.

So how were the performance issue fixed ?

This problem was fixed by creating a virtual DOM. The virtual DOM is a copy of the real DOM stored in memory. When the user interacts with the web page and the state of the web page is updated, a new virtual DOM is created in memory. This new virtual DOM is compared to the previous virtual DOM to identify changes, and these changes are applied to the real DOM to be shown to the user.

This is the working logic of the modern libraries you use and uses the dif algorithm for comparison. they all use virtual DOM. But although the virtual DOM approach is fast, there are points that need to be considered for performance. A change in the virtual domain renders the entire web page Libraries have hooks for performance optimizations

Conclusion

in this article, we have examined what dom and virtual DOM are. We found out why virtual dom is used. Now you know what dom and virtual dom are.

The above is the detailed content of Why Virtual DOM: Render and Performance. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template