如果没有观察到状态,虚拟DOM实现与createDocumentFragment()的不同之处是什么? [英] How's Virtual DOM implementation is different than createDocumentFragment() if no state is observed?

查看:437
本文介绍了如果没有观察到状态,虚拟DOM实现与createDocumentFragment()的不同之处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Virtual DOM是DOM的轻量级副本,在将其插入实际DOM之前在本地维护/缓存。我们可以根据需要更改它,然后保存到我们真正的DOM树中。它使用高效的diff算法来更新来回更改和其他用例。

这一切都是为了避免直接操作DOM,因为它是一项昂贵的操作。
我们有 document.createDocumentFragment()方法,可以在JavaScript中使用,创建想要插入DOM的虚构树节点对象。

我想知道,如果我没有需要观察任何状态或双向绑定的视图/组件(例如只是通过传递选项渲染模板,并处理DOM上的事件),Virtual DOM是否存在在这种情况下真的会有所作为吗?
或者它与createDocumentFragment()一样好,如果它只需要渲染而不是观察状态。

Virtual DOM is a light weight copy of DOM, maintained / cached locally before inserting it into actual DOM. We can change it as we want and then save to our real DOM tree. It uses efficient diff algorithms to update changes back and forth and other use cases.
This all is done to avoid direct manipulation with DOM as it's an expensive operation.
We have document.createDocumentFragment() method which can be used in JavaScript, which also creates imaginary tree node objects to insert into DOM.
I would like to know, if I do not have view / component which need to observe on any state or bidirectional binding(e.g. just render template by passed options, and handle events on DOM), does Virtual DOM will really make a difference in such scenarios?
Or it is as good as createDocumentFragment() if all it has to do is just rendering and no observing on state.

推荐答案

最简单的答案是NodeJS( document.createDocumentFragment ,也不是 document.createElement 或任何此类事情。

The simplest answer is that NodeJS does(/will) not have document.createDocumentFragment, nor document.createElement or any such thing.

VirtualDOM的要点是允许的不仅会对稍后注入DOM的系统进行大规模编辑,而且还允许在不存在DOM的环境中进行任何编辑。

The point of VirtualDOM is to allow for not only large-scale edits to systems where DOM will later be injected, but also to allow for any edits in an environment where the DOM just plain does not exist.

这是 DocumentFragment 和VirtualDOM的实际应用之间的最大差异。

This is the largest difference between practical application of DocumentFragments and VirtualDOM.

增加的好处在DOM虚拟化的特定实例方面,与手动插入片段及其子代相比,某些视图库(React,比如说)使处理这些事情变得非常简单。

Added benefits in terms of specific instances of DOM virtualization would be that certain view libraries (React, say) make dealing with these things quite simple, compared to manual insertion into fragments and their children.

这篇关于如果没有观察到状态,虚拟DOM实现与createDocumentFragment()的不同之处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆