React virtual DOM explained in simple English/简单语言解释React的虚拟DOM
初学React,其中一个很重要的概念是虚拟DOM,看了一篇文章,顺带翻译一下。
If you are using React or learning React, you must have heared of the term `Virtual DOM`. Now what is Virtual DOM and why does react use it?
如果你正在用或者学习React,你一定听过虚拟DOM这个词儿。那什么是虚拟DOM? React为啥要用它呢?
Real DOM
First things first, DOM stands for "Document Object Model". The DOM in simple words represents the UI of your application. Everytime there is a change in the state of your application UI, the DOM get updated to represent that change.
Now the catch is frequently manipulating the DOM affects performance, making it slow.
真实DOM
首先,DOM是"Document Object Model"的缩写,即文档对象模型。简单来说,DOM代表着应用程序的UI。每次当应用程序UI的状态发生变化,DOM就会更新。
现在的问题是频繁操作DOM会影响性能,使其变慢。
What makes DOM manipulation slow?
The DOM is represented as a tree data structure. Because of that, the updates and changes to the DOM are fast. But after that change, the updated element and it's children have to be re-rendered to update the application UI. The re-re-rendering or re-painting of the UI is what makes it slow. Therefore, the more UI you have, the more expensive the DOM updates could be, since they would need to be re-rendered for every DOM update.
什么导致的DOM操作慢?
我们用树结构来表示DOM。因此,对DOM的更新和修改是很快的。不过DOM变了以后,被更新的元素和它所有的子节点都必须重新渲染才能更新UI。正是UI重新渲染导致DOM操作慢。所以,如果UI越多,DOM更新就越慢,因为每个DOM更新都需要重新渲染。
Virtual DOM
That is where the concept of Virtual DOM comes in and performs significantly better than real DOM. The Virtual DOM is only a virtual presentation of the DOM. Everytime the state of our application changes, the Virtual DOM gets update instead of the real DOM.
Well, you may ask `Isn't the virtual DOM doing the same thing as real DOM, this sounds double work? How can this be faster than just updating the real DOM?`
The answer is virtual DOM is much faster and efficient, here is why.
虚拟DOM
这就是虚拟DOM概念的由来,并且比真实DOM效率更高。虚拟DOM只是DOM的虚拟表现形式。每当我们的应用状态发成改变,虚拟DOM就会更新而不是真实DOM。 那么问题来了,你可能会问,虚拟DOM岂不是跟DOM干的事儿一样?看起来做了重复工作,那它怎么能会比只更新真实DOM更快呢?
答案就是虚拟DOM更快更有效率。
How is Virtual DOM faster
When new elements are added to UI, the virtual DOM which is represented as a tree is created. Each element is a node on this tree. If the state of any of these elements changes, a new virtual DOM tree is created. This tree is then compared or `differed` with the previous Virtual DOM tree.
Once this is done, the virtual DOM calculates the best possible method to make the changes to the real DOM. This ensures that there are minimal operations on the real DOM. Hence, reducing the performance cost of updating the real DOM.
The picture below shows the virtual DOM tree and diffing precess
当UI上新增元素时,一个树结构的虚拟DOM就会被创建出来。每个元素都是这个树结构的节点。如果这个树上的任意一个节点发生改变,一个新的虚拟DOM树会被创建。然后这个树将会和之前的树做比对。
一旦这些都完成了,虚拟DOM会计算出一个最优的方法来更新真实DOM。这就保证了在真实DOM上的操作是最少的。因此会减少更新真实DOM的性能损耗。
下图说明了虚拟DOM树和比对过程

source: https://www.oreilly.com/library/view/learning-react-native/9781491929049/ch02.html
The red circles represent the nodes that have changed. These nodes represent the UI elements that have had their state changed. The difference between the previous version of virtual DOM tree and current virtual DOM tree is calculated. The whole parent subtree is then gets re-rendered to give the updated UI. This updated tree is then batch updated to the real DOM.
其中红色的圆圈表示有改变的节点。这些节点代表UI上发生过状态变化的元素。然后当前虚拟DOM树和未发生改变的虚拟DOM树之间的差异就可以计算出来了。然后所有子树的父节点都会重新呈现来更新UI。更新过的树就会批量更新到真实DOM.
How does React use Virtual DOM
Now that you have a fair understanding of what Virtual DOM is, and how it can help with performance of you app, lets look into how React leverages the Virtual DOM.
In React, every piece of UI is a component, and each component has a state. React follows the Observable Pattern and listens for state change. When the state of a component changes, React updates the Virtual DOM tree. Once the VIrtual DOM has been updated, React then compares the current version with previous version Virtual DOM. This process is called `diffing`.
React virtual DOM explained in simple English/简单语言解释React的虚拟DOM的更多相关文章
- React Virtual DOM Explained in Simple English
If you are using React or learning React, you must have heard of the term “Virtual DOM”. Now what is ...
- 直接操作DOM一定比虚拟DOM操作耗时,diff算法,key值,虚拟 DOM的定义
直接操作DOM一定比虚拟DOM操作耗时吗? 或者一次直接DOM操作一定比一次虚拟DOM操作耗时吗? 1)虚拟DOM的本质就是一个JS对象,虚拟DOM减少了真实DOM的操作,当修改数据的时候,就是修改虚 ...
- 深入理解react中的虚拟DOM、diff算法
文章结构: React中的虚拟DOM是什么? 虚拟DOM的简单实现(diff算法) 虚拟DOM的内部工作原理 React中的虚拟DOM与Vue中的虚拟DOM比较 React中的虚拟DOM是什么? ...
- React虚拟DOM浅析
在Web开发中,需要将数据的变化实时反映到UI上,这时就需要对DOM进行操作,但是复杂或频繁的DOM操作通常是性能瓶颈产生的原因,为此,React引入了虚拟DOM(Virtual DOM)的机制. 什 ...
- 实现一个简单的虚拟DOM
现在的流行框架,无论React还是Vue,都采用虚拟DOM. 好处就是,当我们数据变化时,无需像Backbone那样整体重新渲染,而是局部刷新变化部分,如下组件模版: <ul class=&qu ...
- Virtual DOM 虚拟DOM的理解(转)
作者:戴嘉华 转载请注明出处并保留原文链接( #13 )和作者信息. 目录: 1 前言 2 对前端应用状态管理思考 3 Virtual DOM 算法 4 算法实现 4.1 步骤一:用JS对象模拟DOM ...
- [react] 什么是虚拟dom?虚拟dom比操作原生dom要快吗?虚拟dom是如何转变成真实dom并渲染到页面的?
壹 ❀ 引 虚拟DOM(Virtual DOM)在前端领域也算是老生常谈的话题了,若你了解过vue或者react一定避不开这个话题,因此虚拟DOM也算是面试中常问的一个点,那么通过本文,你将了解到如下 ...
- react 的虚拟dom
前端优化的主要方面就是减少页面的DOM操作,减少重排和重绘,React在这方面做了优化,采用了所谓的虚拟DOM,其实我们平时也会遇到虚拟DOM,只是你没有注意罢了,请听我娓娓道来. 所谓的虚拟DOM ...
- 【React 7/100 】 虚拟DOM和Diff算法
虚拟DOM和Diff算法 React更新视图的思想是:只要state变化就重新渲染视图 特点:思路非常清晰 问题:组件中只有一个DOM元素需要更新时,也得把整个组件的内容重新渲染吗? 不是这样的 理想 ...
随机推荐
- windows下安装node.js及环境配置、部署项目
windows下安装node.js及环境配置.部署项目 一.总结 一句话总结: 安装nodejs软件:就像普普通通的的软件那样安装 配置nodejs安装的全局模块的环境变量 并且安装cnpm(比如cn ...
- 无法访问com.alibaba.fastjson.parser.deserializer.PropertyProcessable
某项目加入了某依赖A,IDEA里编译报了如下错误: 无法访问com.alibaba.fastjson.parser.deserializer.PropertyProcessable 错误代码行为某个使 ...
- kotlin中类型检查和类型转换
is 和!is操作符,可以在运行时检查一个对象与一个给定的类型是否一致,或者使用与它相反的!is操作符 fun main(arg: Array<String>) { var a :Any= ...
- Spark2.0协同过滤与ALS算法介绍
ALS矩阵分解 一个 的打分矩阵 A 可以用两个小矩阵和的乘积来近似,描述一个人的喜好经常是在一个抽象的低维空间上进行的,并不需要把其喜欢的事物一一列出.再抽象一些,把人们的喜好和电影的特征都投到这个 ...
- Qt编写数据可视化大屏界面电子看板10-改造QCustomPlot
一.前言 为了抛弃对QChart的依赖,以及echart的依赖,(当然,后期也会做qchart的版本和echart的版本,尤其是echart的版本是肯定会做的,毕竟echart的效果牛逼的一塌糊涂,全 ...
- mac 查看隐藏文件及快速打开终端
查看隐藏文件: 1.在目标目录打开终端,然后输入ls -al命令快速查看目标目录下的文件(包括隐藏文件) 2.快捷键shift+cmmand+.(显示或者隐藏) 打开终端方式: 1.设置组合快捷键,单 ...
- python-Web-flask-蓝图和单元测试
4 蓝图和单元测试: 能够使用代码实现蓝图对项目进行模块化 admin=Blueprint('admin',__name__) # 创建一个蓝图对象 @admin.route('/') def adm ...
- 联想拯救者电脑装Ubuntu没有WIFI
最近给联想电脑装Ubuntu系统,但是装完之后总是无法启动WIFI,而宽带上网却可以,给出一个解决办法,但是该办法应该只适合联想电脑,其他电脑请自测! 打开终端,输入下面指令: sudo modpro ...
- vscode使用Chrome浏览器调试不好用,解决方法!!
1.如果你是刚下载vscode,那么你需要下载两个插件. 1. open in browser(在浏览器中查看,支持五大主流浏览器),下载并启用. 2. view-in-browser (在浏览器中查 ...
- 集群架构03·MySQL初识,mysql8.0环境安装,mysql多实例
官方网址 https://dev.mysql.com/downloads/mysql/社区版本分析 MySQL5.5:默认存储引擎改为InnoDB,提高性能和可扩展性,增加半同步复制 MySQL5.6 ...