简体   繁体   English

带有剔除JS的DOM虚拟化,CSS显示:无

[英]DOM virtualization with knockoutJS, CSS display:none

I wanted to post a widget I have been working on to create a virtualized list with KnockoutJS, and find out any optimizations that I might be missing. 我想发布一个我一直在努力用KnockoutJS创建虚拟列表的小部件,并找出我可能缺少的任何优化。

I am getting mixed answers researching using CSS display:none to virtualize off-screen elements. 使用CSS display:none虚拟化屏幕外元素时,我得到的答案是混合的。 I have heard that in doing so, you avoid the browser "render" cycle, but the elements still get touched in the "dom" update cycle. 我听说这样做可以避免浏览器的“渲染”周期,但是在“ dom”更新周期中仍然会触及元素。 In my testing, it seems that setting display:none (by binding knockout visible) certainly sped up adding an initial amount of items. 在我的测试中,似乎设置display:none (通过绑定剔除可见)肯定会加速添加初始数量的项目。

Finally, in my virtualization binding handler I am currently getting the current viewPort by getting the scroll location and the window height. 最后,在我的虚拟化绑定处理程序中,我当前通过获取滚动位置和窗口高度来获取当前的viewPort。 This allows me to set the in-window index (indexes * rowHeight) to display:block . 这使我可以将窗口内索引(索引* rowHeight)设置display:block However, to hide the other non-visible indices, I iterate through and set each to display:none . 但是,为了隐藏其他不可见的索引,我进行遍历并将每个索引设置为display:none This is obviously O(n) to perform the entire operation, but I don't see another way around it. 这显然是执行整个操作的O(n),但我看不到其他解决方法。 Is there a way to optimize this? 有没有一种方法可以对此进行优化? Would it be better to offload the resetting display:none to a queue, since it is not imperative that it happens right away? 最好将重置显示:none卸载到队列中,因为它不必立即发生?

http://jsbin.com/axupap/56/edit http://jsbin.com/axupap/56/edit

Any insight would be greatly appreciated! 任何见解将不胜感激!

It does not make sense to set the to display none. 将设置为不显示是没有意义的。 To achieve better performance use "if" binding, this way element's are removed from the DOM completely, and that's where you get all performance gains, browser needs to do much less work with smaller tree, to repaint and reflow it's much easier. 为了使用“ if”绑定获得更好的性能,这种方式将元素完全从DOM中删除,这就是您获得所有性能提升的地方,浏览器需要用较小的树做更少的工作,重新绘制和重排它要容易得多。

For example see this Big Scroll demo, if you would putt all items into the tree browser would be crawling. 例如,请参见此Big Scroll演示,如果您将所有项目放入树形浏览器,则会进行爬网。

The best thing you could do on such an inconsistent and worst platform like the web is to make the browser render only the elements which falls in the viewport. 在像Web这样的不一致和较差的平台上,您可以做的最好的事情是使浏览器仅渲染落在视口中的元素。 I would suggest the following algorithm. 我建议以下算法。

      1) get the current scroll height
      2) compute the index of the element at the top
      3) get the remaining elements (using offsets from top index)
      4) push these to the rendering queue which finally renders the elements.

This way you wouldn't touch the if binding, which is very heavy and re-evaluates all the sibling bindings unnecessarily. 这样,您就不会碰到if绑定,因为if绑定非常繁重,并且不必要地重新评估了所有同级绑定。

One more thing, do not use knockout foreach bindings or template bindings if you are dealing with large volume of data. 还有一件事,如果要处理大量数据,请不要使用敲除foreach绑定或模板绑定。 instead use pure javascript and the dom API provides you with something called "DocumentFragment" which improves the performance a lot. 而是使用纯JavaScript,而dom API为您提供了一个称为“ DocumentFragment”的东西,可以大大提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM