jquery: speed difference between .css vs append to head?

后端 未结 3 1591
再見小時候
再見小時候 2021-01-26 22:18

We have some kludgy workarounds for an internal app and I was wondering which jQuery/css technique is considered faster / more efficient ...etc.

Technique A

相关标签:
3条回答
  • 2021-01-26 22:50

    Offsite css is the way to go. with offsite css the browser only needs to make one request which is usually pretty fast.

    on the other hand whenever you modify css elements, the browser must reparse the entire dom tree which is probably more expensive.

    The other important question is:

    Is speed really as important here as you think it is? Maybe the difference is 45ms between these solutions, which is hardly anything you should care about. You should choose a technique which allows you to write code faster and maintain it better, thats where the real time savings will come in.

    0 讨论(0)
  • 2021-01-26 22:59

    I would add non-semantic classes via jQ. Such as: 'float-right', 'margin-right'. This is a lot cleaner to maintain imo.

    0 讨论(0)
  • 2021-01-26 23:05

    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

    enter image description here

    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.

    0 讨论(0)
提交回复
热议问题