Skip to content

Commit 06590c5

Browse files
committed
feat: update vdom-diff docs
1 parent 3b7779d commit 06590c5

File tree

10 files changed

+1119
-772
lines changed

10 files changed

+1119
-772
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1.0.0 (2020-03-20)
1+
# 1.0.0 (2020-03-22)
22

33

44
### Bug Fixes
@@ -58,11 +58,12 @@
5858
* update netlify status ([013b5ca](https://github.com/Rain120/awesome-javascript-code-implementation/commit/013b5ca5847873adabbff93a89daa41500ccf153))
5959
* update readme for plans ([b427b96](https://github.com/Rain120/awesome-javascript-code-implementation/commit/b427b9667cc97c77df75ef0af56eb84b6b4c3ba6))
6060
* update test.yml -> ci.yml and set command ([43b96b6](https://github.com/Rain120/awesome-javascript-code-implementation/commit/43b96b63fc2788a5ea37fa7567c6a6dced4b875f))
61-
* update xmind ([4256758](https://github.com/Rain120/awesome-javascript-code-implementation/commit/4256758c5fe0a036ce3ececc43f848e48964d7a0))
62-
* update xmind ([cb23933](https://github.com/Rain120/awesome-javascript-code-implementation/commit/cb23933f46152c59a7b08f84d7355b35bba25002))
6361
* update xmind ([71317c6](https://github.com/Rain120/awesome-javascript-code-implementation/commit/71317c67376bc8196f80e25391adfdc9b2496380))
6462
* update xmind ([408ca68](https://github.com/Rain120/awesome-javascript-code-implementation/commit/408ca685142dc425e90be64a284016d57d580893))
63+
* update xmind ([4256758](https://github.com/Rain120/awesome-javascript-code-implementation/commit/4256758c5fe0a036ce3ececc43f848e48964d7a0))
64+
* update xmind ([cb23933](https://github.com/Rain120/awesome-javascript-code-implementation/commit/cb23933f46152c59a7b08f84d7355b35bba25002))
6565
* update: plans; fix: readme bug ([d44e559](https://github.com/Rain120/awesome-javascript-code-implementation/commit/d44e559def7834fcccbb19797a1d29bf5cc4e5b7))
66+
* vdom diff algorithm ([3b7779d](https://github.com/Rain120/awesome-javascript-code-implementation/commit/3b7779dcad7e184027d1bddfa6387224c2f009d3))
6667
* vuepress docs v1.0.0 ([785caeb](https://github.com/Rain120/awesome-javascript-code-implementation/commit/785caeb41beeff7a0f53646fc952d1c89ebb61d5))
6768
* zh-cn and plans ([62b460f](https://github.com/Rain120/awesome-javascript-code-implementation/commit/62b460f200a2f76ee36cd3cae94d1660ed4ff3c2))
6869

docs/zh/vdom-diff/README.md

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,83 @@
11
## 原理
22

3+
### 常规 DOM 渲染
34

5+
![layout-paint.png](~@images/src/vdom-diff/images/layout-paint.png)
6+
7+
我们知道目前所有的浏览器都是遵循类似上面图片所绘制出的工作流, 仅在细节处略有不同。
8+
9+
- 创建`DOM树`
10+
11+
一旦浏览器接收到一个`HTML`文件, 渲染引擎 `(render engine)` 就开始解析它, 并根据 HTML 元素 (elements)一一对应地生成`DOM节点 (nodes)`, 组成一棵`DOM树`
12+
13+
- 创建渲染树
14+
15+
同时, 浏览器也会解析来自外部`CSS文件`和元素上的`inline`样式。通常浏览器会为这些样式信息, 连同包含样式信息的`DOM树`上的节点, 再创建另外一个树, 一般被称作渲染树 `(render tree)`
16+
17+
- 创建渲染树背后的故事
18+
19+
`WebKit`内核的浏览器上, 处理一个节点的样式的过程称为`attachment``DOM树`上的每个节点都有一个`attach`方法, 它接收计算好的样式信息, 返回一个`render`对象 (又名`renderer`)
20+
21+
`Attachment` 的过程是同步的, 新节点插入 `DOM树`时, 会调用新节点的`attach`方法。
22+
构建渲染树时, 由于包含了这些 render 对象, 每个 render 对象都需要计算视觉属性 `(visual properties)`; 这个过程通过计算每个元素的样式属性来完成。
23+
24+
- 布局 `Layout`
25+
又被简称为`Reflow`
26+
构造了渲染树以后, 浏览器引擎开始着手布局 `(layout)`。布局时, 渲染树上的每个节点根据其在屏幕上应该出现的精确位置, 分配一组屏幕坐标值。
27+
- 绘制 `Painting`
28+
29+
接着, 浏览器将会通过遍历渲染树, 调用每个节点的 `paint` 方法来绘制这些 `render` 对象。`paint` 方法根据浏览器平台, 使用不同的 `UI后端API` `(agnostic UI backend API)`。 通过绘制, 最终将在屏幕上展示内容。
30+
31+
`DOM操作` 真正的问题在于每次操作都会触发布局的改变、`DOM树` 的修改和渲染。`Virtual DOM`把管理 `DOM碎片`这件事情自动化、抽象化了, 使得你无需再去手动处理。
32+
33+
### 什么是 Virtual DOM?
34+
35+
`Virtual DOM`是对`DOM`的抽象, 本质上是`JavaScript`对象, 这个对象就是更加轻量级的对`DOM`的描述.
36+
37+
### 传统的 diff 算法 和 React 的 diff 算法
38+
39+
- 传统 `diff` 算法的复杂度为`O(n^3)`
40+
41+
- `React diff`算法的复杂度为`O(n)`
442

543
### 先序深度遍历
644

745
- 先中后序遍历 -> 遍历根节点的顺序
846

9-
- 先序 -> 根, 左, 右
47+
- `先序 -> 根, 左, 右`
1048

11-
- 中序 -> 左, 根, 右
49+
- `中序 -> 左, 根, 右`
1250

13-
- 后序 -> 左, 右, 根
51+
- `后序 -> 左, 右, 根`
1452

1553
- 深度(DFS)遍历 -> 从根节点出发, 沿着左子树方向进行纵向遍历, 直到找到叶子节点为止。然后回溯到前一个节点, 进行右子树节点的遍历, 直到遍历完所有可达节点为止。
1654

1755
- 广度(BFS)遍历 -> 从根节点出发, 在横向遍历二叉树层段节点的基础上纵向遍历二叉树的层次。
1856

1957
![dom-diff-alogrithm.png](~@images/src/vdom-diff/images/dom-diff-alogrithm.png)
2058

59+
### DOM Diff 算法策略
60+
61+
![dom-diff-algorithm.svg](~@images/src/vdom-diff/images/dom-diff-algorithm.svg)
62+
63+
### Mini Diff 算法实现
64+
65+
- 替换
66+
- 节点替换
67+
- 属性替换
68+
- 文本替换
69+
- 更多尚未实现
70+
71+
- 移除
72+
- 节点移除
73+
- 属性移除
74+
- 文本移除
75+
- 更多尚未实现
76+
77+
### Dom Diff 流程
78+
79+
![dom-dff.png](~@images/src/vdom-diff/images/dom-dff.png)
80+
2181
## Demo
2282

2383
[Link](https://rain120.github.io/vdom-diff-algorithm/)
@@ -48,19 +108,38 @@
48108
49109
<<< @/src/vdom-diff/src/patch.ts
50110
51-
52111
## 参考
53112
113+
### Diff 相关
114+
115+
[Diff Strategies](https://neil.fraser.name/writing/diff/)
116+
54117
[React Diffing 算法](https://zh-hans.reactjs.org/docs/reconciliation.html#the-diffing-algorithm)
55118
56119
[React's diff algorithm - Christopher Chedeau](https://calendar.perfplanet.com/2013/diff/)
57120
58121
[React Dom Diff](https://sekaiamber.github.io/react-dom-diff/)
59122
60-
[virtual-dom](https://github.com/Matt-Esch/virtual-dom)
123+
[Under-the-hood-ReactJS](https://github.com/Bogdan-Lyashenko/Under-the-hood-ReactJS)
124+
125+
[babel-plugin-transform-react-jsx](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx/)
126+
127+
[diff 算法原理概述](https://github.com/NervJS/nerv/issues/3)
128+
129+
[React 源码剖析系列 - 不可思议的 react diff](https://zhuanlan.zhihu.com/p/20346379)
130+
131+
### Virtual DOM 相关
132+
133+
[snabbdom](https://github.com/snabbdom/snabbdom/blob/master/src/snabbdom.ts)
61134
62135
[How to write your own Virtual DOM](https://medium.com/@deathmood/how-to-write-your-own-virtual-dom-ee74acc13060)
63136
64-
[Under-the-hood-ReactJS](https://github.com/Bogdan-Lyashenko/Under-the-hood-ReactJS)
137+
[Mini Virtual DOM 实现](https://github.com/yelouafi/petit-dom)
65138
66-
[babel-plugin-transform-react-jsx](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx/)
139+
[virtual-dom](https://github.com/Matt-Esch/virtual-dom)
140+
141+
[解析 snabbdom 源码](https://github.com/creeperyang/blog/issues/33)
142+
143+
[为什么要使用 Virtual DOM](https://hashnode.com/post/the-one-thing-that-no-one-properly-explains-about-react-why-virtual-dom-cisczhfj41bmssp53mvfwmgrq) -> [中文版](https://www.zcfy.cc/article/the-one-thing-that-no-one-properly-explains-about-react-why-virtual-dom-hashnode-1211.html)
144+
145+
[浏览器的工作原理:新式网络浏览器幕后揭秘](https://www.html5rocks.com/en/tutorials/internals/howbrowserswork/)

plans.svg

Lines changed: 765 additions & 757 deletions
Loading

plans.xmind

17.3 KB
Binary file not shown.

src/vdom-diff/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 原理
1+
## 虚拟DOM 相关解释
2+
3+
[vdom-diff](https://rain120.github.io/awesome-javascript-code-implementation/zh/vdom-diff/)
4+
5+
## 实现原理
26

37
### 通过 JavaScript对象来描述真实DOM
48

@@ -122,10 +126,6 @@ function patch(parent, patches, index = 0) {
122126
}
123127
```
124128

125-
### 先序深度遍历
126-
127-
![dom-diff-alogrithm.png](./images/dom-diff-alogrithm.png)
128-
129129
## Demo
130130

131131
[Link](https://rain120.github.io/vdom-diff-algorithm/)
154 KB
Binary file not shown.

src/vdom-diff/images/dom-dff.png

64.9 KB
Loading

0 commit comments

Comments
 (0)