views:

443

answers:

7

Back in 2005, Quirksmode.com released this article:

http://www.quirksmode.org/dom/classchange.html

that showed "proof" that changing the style of an element by changing its class (ie. "elem.className = x") was almost twice as fast as changing its style via its style property (ie. "elem.style.someStyle = x"), except in Opera. As a result of that article, we started using a className-based solution to do things like showing/hiding elements on our site.

The problem is, one of our developers would much rather use jQuery's equivalent methods for this kind of thing (ie. "$(something).hide()"), and I'm having a hard time convincing him that our className-based function is worth using, as I can only find a single article written four years ago.

Does anyone know of any more recent or more comprehensive investigations in to this issue?

A: 

No, but I can tell you that jQuery.hide() can hide an element with a smooth effect. This cannot be done by changing the className.

Maurice Perry
Yes it can be done by changing the className... Just animate the className. Granted, it's more difficult...
strager
+5  A: 

Micro-optimization is evil. I think unless you are hiding a seriously large amount of elements at once or something, the difference in milliseconds is unimportant if by some chance that article is still relevant nowadays.

With that in mind, I would go with jQuery's methods as they are battle tested and more concise.

Paolo Bergantino
+2  A: 

While I generally agree with the practice of using classes over style attributes for many reasons. Performance is one but consistency is another. I've often seen people do things like:

function toggle(element) {
  element.style.display = element.style.display == 'none' ? 'block' : 'none';
}

(or the jQuery equivalent)

Seems reasonable right? It is until you apply it to table elements that have default display values of, for example, table-cell (not block). The class method:

.hidden { display: none; }
...
function toggle(element) {
  $(element).toggleClass("hidden");
}

is much, much better for this and other reasons.

But the jQuery methods like hide() are an exception to this. They handle display setting correctly and give you the animations.

cletus
Note that ... ? '' : 'none'; will work correctly in your sample.
Aistina
No it won't, although it may seem to. TDs have a default display property of "table-cell", not "block", so if you use that code you wind up (essentially) saying to the browser "this TD isn't a TD anymore, it's now a block-level element, like a DIV"; this will have (subtle) effects on its rendering.
machineghost
A: 

You are talking about client code, it runs in the user browser so it does not load your server. I ignore the javascript in your client side but I guess is not crunching much CPU.

Your coleague use of jQuery is probably not having a great impact while it results in a more readable code. Therefore I think he does not need to be convinced at all.

Miquel
+2  A: 

Test it yourself. There's a tester on the page you linked. Test performance on your target browsers to determine the best method to use, performance-wise.

Personally, I would choose readability over performance. Besides, the tides may turn later (if they haven't already**). If it makes sense to use a class (i.e. you use the style for many elements), you might as well use a class. If the CSS is for an animation on an element, use style. For either, prefer jQuery's functions as they are (1) more consistent and (2) more robust and tested.

**For Opera 10, at least, the speed has definitely improved. The tests are 5/12ms locally for Opera 10, 57/88ms for Firefox 3, 14/36ms for Google Chrome, and 125/118ms (!) for IE7. IE7 (perhaps your target browser) has about the same speed for either, with style changing slightly in the lead!

Premature optimization is the root of all evil.

strager
A) I'm not 100% confident in that benchmark's methodologyB) I was (ideally) looking for a more comprehensive assessment of the "real" performance benefits, not just a measurement of the performance of a single JS operation (like many here, my co-worker also chants "micro-optimization == evil" ;-))
machineghost
+3  A: 

There is a flaw in the benchmark that article uses.

In my personal experience I've never seen a case where updating a className outperforms inline style setting. I have no concrete proof of this (I do vaguely remember an article I'm going to try to dig up), but I have noticed that large clientside apps (for example gmail, or google maps) prefer setting inline styles to classNames, and it was in the context of analysis of these apps that I first heard of the speed increase in doing so.

Note that I am not promoting one over the other: setting the className dynamically goes a long way in terms of maintainability/readability and separating concerns.

Crescent Fresh
+1  A: 

Are you hiding hundreds of elements per second? If not, I'd say there are bigger fish to fry. Worse, a study done in 2005 says nothing about the performance of modern browsers. No IE7 or 8, No Firefox 3 (was 2 even out?), no Chrome.

But if you insist and want your fellow colleagues to follow suit, you should write a jQuery plugin that "hide()"'s using a class instead of a style.

Cory R. King
I understand that a 2005 study is not so helpful ... that was sort of the entire point of this question ;-)As for the plug-in idea, I totally agree; if we continue to use classes for this, we will almost certainly make a jQuery plug-in (we already had an in-house function before).
machineghost
P.S. While I don't think we were hiding "hundreds of elements per second" we did have a very real performance problem on a page in the past, and switching to class-based showing/hiding did make a significant difference on it (which is why we started using it in the first place).
machineghost