Firefox Jquery appendTo 效率低下?
我正在重新设计一个作品集,我有一些 JS 可以生成大量(大约 300 个)div,设置它们的样式,并将它们附加到正文。这在 webkit 浏览器中运行得很快而且完美,但在 Firefox 中却慢得要命。
我一直试图找出为什么 Firefox 无法处理这个问题,并且我尝试将所有 div 的 html 作为字符串连接起来,然后将整个内容附加到 body 中,但事实证明这同样慢或更慢。
如果您想实时查看问题,我的网站是此处
这是相关的代码:
get_bokeh 返回 a描述单个“散景”片段的 CSS 样式字符串。
function generate(){
$("#bokeh_container").remove();
if (q==0){
min = 30,
max = 30,
bokeh_count = 1;
}
else if (q==1){
min = 7,
max = 10,
bokeh_count = 300;
}
else if (q==2){
min = 7,
max = 15,
bokeh_count = 300;
}
else if (q==3){
min = 8,
max = 11,
bokeh_count = 500;
}
sum = min+max;
window_width = $(document).width();
window_height = $(window).height();
colorful = $("#colorful").attr("checked");
var container = $("<div />",{"id":"bokeh_container","style":"width:100%; height:100%; position:absolute; left:50%; margin-left:-600px; top:0px; z-index:1; display:none; "});
for( var i=0;i<bokeh_count;i++){
$("<div />",{"class":"bokeh","style":get_bokeh()}).appendTo(container);
}
container.appendTo("body").show();
I'm working on a portfolio redesign and I have a bit of JS that generates a large number (around 300) of divs, styles them, and appends them to body. This works quickly and perfectly in webkit browsers, but when it comes to Firefox it's slow as hell.
I've been trying to figure out why Firefox can't handle this, and I tried concatenating all the divs' html as strings and then appending the whole thing to body, but this proved to be just as slow or slower.
If you'd like to see the problem live, my site is here
Here's the relevant bits of code:
get_bokeh returns a string of CSS styles describing a single "bokeh" piece.
function generate(){
$("#bokeh_container").remove();
if (q==0){
min = 30,
max = 30,
bokeh_count = 1;
}
else if (q==1){
min = 7,
max = 10,
bokeh_count = 300;
}
else if (q==2){
min = 7,
max = 15,
bokeh_count = 300;
}
else if (q==3){
min = 8,
max = 11,
bokeh_count = 500;
}
sum = min+max;
window_width = $(document).width();
window_height = $(window).height();
colorful = $("#colorful").attr("checked");
var container = $("<div />",{"id":"bokeh_container","style":"width:100%; height:100%; position:absolute; left:50%; margin-left:-600px; top:0px; z-index:1; display:none; "});
for( var i=0;i<bokeh_count;i++){
$("<div />",{"class":"bokeh","style":get_bokeh()}).appendTo(container);
}
container.appendTo("body").show();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该删除 for 循环中的
.appendTo
。您告诉浏览器在每次迭代时创建对 dom 的添加,这是昂贵的。相反,将对象添加到数组或将它们连接到字符串(便宜得多),然后再执行一次追加。You should remove the
.appendTo
in your for loop. You are telling the browser to create the add to the dom every iteration which is expensive. Instead add the objects to an array or concat them to a string (much cheaper) then do a single append afterwards.查看此 jsperf 测试:http://jsperf.com/test-of-jquery-appendto。
将 HTML 构建为字符串,然后将其一次性添加到 DOM,在 Chrome 和 Firefox 中速度提高了 2-3 倍,在 IE8 中速度提高了近 5 倍。这并不是对您正在做的事情的完美模拟,但可能值得一看。
Check out this jsperf test: http://jsperf.com/test-of-jquery-appendto.
Building the HTML into a string and then adding it to the DOM all at once shows as 2-3x faster in Chrome and Firefox and almost 5x faster in IE8. This isn't a perfect simulation of what you're doing, but probably worth looking at.
你的代码在 FF 中并不比在 Chrome 中慢多少。
查看并运行它的性能测试。
另外,您可能想要执行标准操作:关闭 Firefox,运行 Ccleaner,重新启动 FF dance。
Your code is not much slower in FF than in Chrome.
See and run this performance test of it.
Also, you might want to do the standard: shut-down Firefox, run Ccleaner, restart FF dance.
为什么不使用 css 类而不是内联样式?我看到您为容器甚至循环中的 div 设置了一堆样式属性。如果您在类中设置这些样式并使用该类,那么它肯定会提高性能,因为 jquery 在创建元素时不必遍历所有属性。
Why dont you use css classes instead of inline styles? I see you have bunch of style attributes set for the container and even for the div in the loop. If you set these styles in class and use the class instead it will definately improve the performance as jquery dont have to iterate through all the properties while creating the element.