简体   繁体   English

太多的html元素减慢了jquery

[英]Too much html element slow down jquery

I have a js script to get a datafeed and built some div according to the response text. 我有一个js脚本来获取数据源并根据响应文本构建一些div。

I have already used web worker and ajax to split the read datafeed and build html code process. 我已经使用了web worker和ajax来拆分读取数据源并构建html代码进程。

However while I append the html to my container, it becomes slow (let say 18000's div) 然而,当我将html附加到我的容器时,它变得很慢(比如18000的div)

Then I change the method to append the 200 div for scroll to down of my container. 然后我更改方法以附加200 div以滚动到我的容器的下方。

It becomes very fast, but while all items render, the other process start to become slow, for example if I make the container show and hide, it is pretty slow. 它变得非常快,但是当所有项目都渲染时,另一个进程开始变慢,例如,如果我让容器显示和隐藏,那么它很慢。

I found another solution is to use z-index, it makes the show hide process much faster. 我发现另一个解决方案是使用z-index,它使show hide进程更快。

However, while I need to change the container content, it becomes slow again. 但是,虽然我需要更改容器内容,但它会再次变慢。

Any idea or solution? 任何想法或解决方案?

在实时DOM结构之外执行尽可能多的更改将提高性能。要了解更多信息,请查看如何在页面中提高java脚本的性能?

I have been in this similar situation, where I was adding lot of child's to a node in a tree. 我一直处于类似的情况,在那里我将很多孩子添加到树中的节点。

Problems 问题

  1. The page was slow. 页面很慢。
  2. Rendering of elements were slow,Sometimes even JS crashes in IE. 元素的渲染很慢,有时甚至JS在IE中崩溃。

Solutions 解决方案

  1. I created a upper threshold of the number elements that will be present in a DOM at one instance. 我创建了一个实例中DOM中存在的数字元素的上限阈值。 Lets say initially there are 500 elements,then there is another ajax request that will be fetching around 500 nodes so I would clear some element. 让我们说最初有500个元素,然后有另一个ajax请求将获取大约500个节点,所以我会清除一些元素。 It can help if you can manage the clearing scenarios. 如果您可以管理清算方案,它可以提供帮助。
  2. No inline CSS Styles, It will look what this thing will create difference but keep all the Styling in CSS classes improves performance. 没有内联CSS样式,它将看起来会产生什么差异,但保留CSS类中的所有样式可以提高性能。
  3. Defining an appropriate HTML Structure Like not keeping any dangling divs,spans etc. 定义合适的HTML结构就像不保留任何悬空div,跨度等。
  4. attaching event handlers through proper delegation like 'on'. 通过像'on'这样的适当委托来附加事件处理程序。

There are lot of things that can be done tom improve this. 汤姆可以做很多事情来改善这一点。 If you can post a jsfiddle for this will certainly help. 如果你可以发布一个jsfiddle,这肯定会有所帮助。

问题解决了SlickGrid可以轻松处理的问题

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

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