• The virtual DOM was created to address performance issues caused by frequent manipulation of the real DOM. It is a lightweight, in-memory representation of the real DOM, which can be later used as a reference to update the actual web page. (View Highlight)
    • Note: 虚拟DOM是为了解决频繁操作真实DOM所引起的性能问题而创建的。它是真实DOM的轻量级、内存中的表示,可以作为更新实际网页的参考。
  • With newer frameworks like Svelte, the virtual DOM isn’t even used because of the performance overhead. Instead, Svelte uses a technique called “dirty checking” to determine what has changed. Fine-grained reactivity frameworks like SolidJS take this a step further by pinpointing exactly what has changed and updating only that part of the DOM. (View Highlight)
    • Note: 使用像Svelte这样的新框架,由于性能开销,甚至不使用虚拟DOM。相反,Svelte使用一种称为“脏检查”的技术来确定发生了什么变化。像SolidJS这样的细粒度响应性框架更进一步,精确定位到底发生了什么变化,并仅更新DOM的该部分。
  • Static Analysis: The virtual DOM is analyzed to extract dynamic parts of the tree into an “Edit Map,” or the list of the “edits” (mappings) of the dynamic parts of the virtual DOM to the state. (View Highlight)
    • Note: 块虚拟DOM使用了不同的差异比较方法,分为两个部分:
      第一步: 静态分析,对虚拟DOM进行分析,将树的动态部分提取到“编辑映射”中,或者将虚拟DOM的动态部分映射到状态的列表中。
  • Dirty Checking: The state (not the virtual DOM tree) is diffed to determine what has changed. If the state has changed, the DOM is updated directly via the Edit Map. (View Highlight)
    • Note: 第二步:脏检查,通过比较状态而不是虚拟DOM树来确定发生了什么变化,如果状态发生了变化,通过编辑映射直接更新DOM
  • You can see that the dirty checking example takes much less computation than the diffing step. This is because the dirty checking step is only concerned with the state, not the virtual DOM, as each virtual node might need many levels of recursion to determine if it has changed, state just needs a shallow equality check. (View Highlight)
    • Note: 脏检查示例所需的计算量比差异检查步骤少得多。这是因为脏检查步骤只关注状态,而不关注虚拟DOM,因为每个虚拟节点可能需要多层递归来确定是否发生了变化,而状态只需要进行浅层相等性检查。
  • Block virtual DOM is best used when there is a lot of static content with little dynamic content. The biggest advantage the block virtual DOM has is that it doesn’t need to think about the static parts of the virtual DOM, so if it can skip over a lot of static content, it can be very fast. (View Highlight)
    • Note: 块虚拟 DOM的适用场景?
      块虚拟 DOM 并不是万能的优化方案,使用的时候需要注意
      块虚拟DOM在静态内容较多、动态内容较少的情况下最为适用。块虚拟DOM的最大优势在于它不需要考虑虚拟DOM的静态部分,因此如果能够跳过大量的静态内容,它可以非常快速。
      但是如果组件的动态内容较多,那么使用块虚拟 DOM 可能看不出性能差异,而且还可能比常规虚拟 DOM 性能更差