jQuery 的交换方法时间成本高昂
我在 firebug 中运行分析器,发现 UI 中运行时间成本最高的函数是 jQuery 的 .swap()。我可以看到这必须与 .css() 方法相关联。
这是我的分析器的图像:
显然,为了提高此处的速度,我需要减少调用 .css (),但这就是一个相当大的项目,因为我用它来设置和纠正不同元素的样式,这些元素必须是完全动态的。
有没有更好的方法来加快速度?
我正在使用jquery.1.4.2。我无法继续使用 1.4.4,因为它出于某种原因破坏了我的很多脚本。将交换方法从 1.4.4 扩展到 1.4.2 有好处吗?或者这会带来更多的不兼容性吗?
什么是交换方法?为什么它的时间成本如此之高?
I ran the profiler in firebug and found that the most time costly function being ran in our UI is jQuery's .swap(). I can see that this must be associated with the .css() method.
Here is an image of my profiler:
Obviously, to improve the speed here I need to cut back on calling .css(), but that is quite the project as I use it to set and correct styling of different elements, which has to be completely dynamic.
Is there a better way to speed this up?
I am using jquery.1.4.2. I haven't been able to move on to 1.4.4 because it breaks quite a few of my scripts for some reason. Would it be be of benefit to extend the swap method from 1.4.4 to 1.4.2? Or would that present more incompatibilities?
What is the swap method and why is it so time costly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从我所看到的 jQuery UI 代码来看,
.swap()
并不是直接被调用的,这是 jQuery 框架本身中唯一使用.swap()
的地方> 直接是检索元素的计算高度/宽度时的.height()
和.width()
调用。当浏览器无法直接提供大小时,它的作用是暂时将样式交换为以下样式:
然后在更改后的样式状态下执行通常的内部
getWH()
函数,并恢复。部分代码借用了 jQuery:
jQuery UI 到处都使用
height()
和width()
函数,因此.swap()
也就不足为奇了。 code> 函数在配置文件中占有重要地位。From what I can see looking at the jQuery UI code,
.swap()
is not called directly by it, the only place in the jQuery framework itself that uses.swap()
directly are the.height()
and.width()
calls when retrieving the computed height/width of an element.What it does to calculate the sizes when not available directly from the browser is temporarily swaps the style out for the following:
Then performs the usual internal
getWH()
function whilst in that altered style state, and restores.The partial code borrowed from jQuery:
jQuery UI uses
height()
andwidth()
functions everywhere, so it is little wonder that the.swap()
function figures highly in the profile.