jquery:.css和append之间的速度差? [英] jquery: speed difference between .css vs append to head?
问题描述
我们有一个内部应用程序的kludgy解决方法,我想知道哪个jQuery / css技术被认为更快/更高效...等。
We have some kludgy workarounds for an internal app and I was wondering which jQuery/css technique is considered faster / more efficient ...etc.
技术使用.css
$( "#doc" ).css( "margin-top", "90px" );
$( "#checkouthelp" ).css( "margin", "50px 50px 0 0" );
$( "#changelanguage" ).css( "float", "right" )
.css( "margin", "0 50px 0 0" );
技巧B 。附上
$("<style>")
.prop("type", "text/css")
.html("\
#doc {\
margin-top:90px;\
}\
#checkouthelp {\
margin:50px 50px 0 0;\
}\
#changelanguage {\
float:right;\
margin:0 50px 0 0;\
}")
.appendTo("head");
这是一个小的,相对无害的项目,但已发展成一个相当膨胀解决方法。 链接到站外css文件更快,或直接更改css规则,因为我在这里。速度是关键问题,上面的示例只是一小部分的增长kludge 。 [感谢,根本原因正在评估...但在此期间]。
This is one of those projects that started out small and relatively innocuous, but has developed into a fairly bloaty workaround. Is it faster to link to an off-site css file or directly change the css rules as I'm doing here. Speed is the critical issue and the above sample is but a small slice of the growing kludge. [ Thankfully, the root cause is being assessed ...but in the meantime ].
谢谢!
UPDATE
虽然我没有进行彻底的测试,但在几个速度测试中,我使用Chrome的开发工具,外部样式表的规则较慢。会做更多的测试,但我想我可能卡住使用 .css
。感谢您对此信息的回复!
Though I haven't done thorough testing, in the few speed tests I've done use Chrome's developer tools, looks like loading rules from an external stylesheet is slower. Will do more testing, but I think I'm probably stuck using .css
. Thanks for the responses to this post!
推荐答案
jQuery的 .css
比你提供的样品更快。我创建了一个测试用例,在此演示: http://jsperf.com / jquery-css-vs-append-to-head
jQuery's .css
is faster than appending to head with the sample you provided. I have created a test case which demonstrates this here: http://jsperf.com/jquery-css-vs-append-to-head
对于异地CSS,它不可比,因为异地css速度取决于用户的互联网连接,您的服务器速度等。如果你有一个恶意的服务器和宽带用户,我说offsite是好得多,否则为速度你只能可靠地依赖javascript。
As far as offsite CSS, its not comparable, because offsite css speed is dependent on the users internet connection, along with your server speed ect. If you have a wicked server and broadband users, I say offsite is much better, otherwise for speed you can only reliably count on javascript.
这篇关于jquery:.css和append之间的速度差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!