Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual DOM?它是如何提升性能的? #35

Open
GGXXMM opened this issue Aug 12, 2019 · 0 comments
Open

Virtual DOM?它是如何提升性能的? #35

GGXXMM opened this issue Aug 12, 2019 · 0 comments
Labels

Comments

@GGXXMM
Copy link
Owner

GGXXMM commented Aug 12, 2019

Virtual DOM

Virtual DOM是虚拟节点,用一个原生JS对象去描述一个DOM节点,最少包含标签名(tag)、属性(props)和子元素(children)。

{
    tag: "div",
    props: {},
    children: [
        "Hello World", 
        {
            tag: "ul",
            props: {},
            children: [{
                tag: "li",
                props: {
                    id: 1,
                    class: "li-1"
                },
                children: ["第", 1]
            }]
        }
    ]
}

上面的Virtual DOM会生成以下的HTML:

<div>
    Hello World
    <ul>
        <li id="1" class="li-1">
            第1
        </li>
    </ul>
</div>

Virtual DOM是如何提升性能的?

Virtual DOM是通过diff算法提升频繁操作DOM节点的性能。视图更新时,diff算法通过比较新老虚拟节点,并做标记,无差异的节点不变,只更新变化的dom节点,减少页面的重排重绘,相比innerHTML粗暴渲染DOM节点大大提升了性能。

@GGXXMM GGXXMM added the vue label Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant