Comments (13)
I am sorry as I can't speak your language, but I am still interested in this topic, would someone please be so kind to translate it to me? (Google Translator doesn't really work well)
from nerv.
@kurtextrem
Check this out:
https://github.com/adamhaile/surplus/blob/5f5ad8d9e7eb6db954cd146bd48772f77886b295/es/content.js
https://github.com/ivijs/ivi/blob/2c81ead934b9128e092cc2a5ef2d3cabc73cb5dd/packages/ivi/src/vdom/implementation.ts#L12:81
These comments should answer your question.
from nerv.
可以发现 a, b, c 能匹配 LIS?
from nerv.
@zhangchen91 改了,谢谢。
from nerv.
这个算法看了下,Inferno 也是这么实现的,可否认为建立『索引 I』是一种空间换时间的优化,然后在 LIS 算法上时间复杂度是 O(n) ?个人看资料也是 O(n log n)啊?
想问下之前几种编辑距离(Edit Distance)应该跟 LCS 是对偶问题吧,似乎优化也是到 O(n log n)?因为我有看到有 vdom 算法是用 LCS https://github.com/yelouafi/petit-dom/blob/5412b18ad26108762ebf5624ac1c5bd70c578e57/src/vdom.js#L690
最短编辑距离和最小移动应该算不同的问题吧?毕竟一个针对的是改插删,而后者更针对移动插删?
最近为了看 Diff 算法补了好几篇论文有点头晕还没消化过来,求解惑~
from nerv.
-
是空间换时间。所以在具体的实现上会设置一个限制,当数组长度太低的时候就不建立索引了,直接遍历全部。大部分性能排行在 js-framwork-bench 前列的框架基本都是用这个算法,ivi 是最早使用的,其他的或多或少都参考了他。
-
目前没发现可以优化到 O(n) 的算法,你给的链接应该是目前已经实现在 vdom 中最快的算法,期望复杂度是 O(n log n)。
-
我个人认为是同一件事情,diff 算法的核心就是找最小的编辑距离,找最小移动只是其中的一个方法而已。
from nerv.
了解,那之前好多文章写 React 的 Diff 算法复杂度是从 O(n^3) 优化到 O(n) 要如何理解呢?
children 的这块的复杂度就已经超越了 O(n) 了不是么?
所以 O(n^3) 到 O(n) 的这个优化度的说法,是不包含 children 的部分?
还是说大部分解读的文章有误读呢?或者说是 React 放弃了 children 这块的优化,取了一个 O(n) 的 Patch 解法?
PS:在知乎提了个问,感觉可以把 issue 的内容贴过去宣传一波,https://www.zhihu.com/question/274459523 ,还挺多人关注的
from nerv.
我猜这类文章指的是整棵树的对比是 O(n ^ 3),而 React 只对比同级元素,并且他们的 type
和 key
是一致才继续 diff (包括他们 children)下去,否则就直接替换或删除掉。从这个意义上来讲,这些文章也没有讲错。
from nerv.
请问,为何最后一步是尾部遍历新数组 B 呢?明明 P 中存储的是元素在旧数组 A 中的位置。而且如果是遍历新数组的话,尾部元素插入到头部中的 a, b, c 为什么能匹配 LIS,明明他们的新位置是 1, 2, 3 ?
时间隔了很久才发现这篇文章,现在提问打扰了。
from nerv.
请问一下,找到LIS之后,可不可以换成这样
- 找到LIS
- 正序遍历新数组,从P中得到元素对应的位置
- 判断元素所在的位置是不是在LIS中
- 如果是,就不移动当前元素,如果不是,就移动当前元素到对应位置。
不知道这样和你的思路是否一样,(因为我没看代码,所以不清楚你的代码是不是这样的思路)
from nerv.
细节太多了,光看这个能懂个80%左右,第3步“找到最小的移动次数”还是看了源码才懂,算法核心已经实现了,感谢作者,受益匪浅~
from nerv.
*
* A: [b c d e f]
* ^
* B: [c b h f e]
* P: [1 0 . . 3] // . == -1
* I: {
* c: 0,
* b: 1,
* h: 2,
* f: 3,
* e: 4, <-
* }
* moved = true
e这步的moved是false ?
from nerv.
* * A: [b c d e f] * ^ * B: [c b h f e] * P: [1 0 . . 3] // . == -1 * I: { * c: 0, * b: 1, * h: 2, * f: 3, * e: 4, <- * } * moved = true
e这步的moved是false ?
按照他的表述确实应该是 false,但是通篇看下来并没发现moved的作用,主要就是通过新旧数组来生成那个变化数组
from nerv.
Related Issues (20)
- jest 多个Effect的时候报错
- jest useEffect中 代码没执行
- [Violation] Added non-passive event listener to a scroll-blocking <some> event. Consider marking event handler as 'passive' to make the page more responsive. See <URL> HOT 1
- 请问现在最新版还能兼容IE8否 HOT 1
- redux无法在ie8 下使用
- this.forceUpdate()
- devtools error HOT 1
- react hooks support? HOT 1
- [bug] portal里的事件没有正确冒泡 events triggered in portal are not propagated
- Suspense / Lazy Support HOT 3
- nervjs1.4.0版本之后safari浏览器,iphone webview里input中文输入异常
- setState的回调函数中再次setState,第二个setState回调函数无法及时获取设置后的值 HOT 4
- nerv和百度地图js sdk 一起使用的时候会死循环
- 【讨论贴】给React加上强制刷新,是种什么体验? HOT 3
- nervjs 1.5.7 typings 路径错误 HOT 1
- nerv-test-utils的api:findRenderedDOMComponentWithClass问题
- [Version 1.5.7] cannot resolve the typing file
- Is that the project name got from EVA? NERV -_-!
- classComponent
- 无法兼容ie8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nerv.