JQuery implicit loops
elijahmanor.com
elijahmanor.com
$( ".widget" ).css( "color", "green" ).css( "font-size", "16px" );
$( ".widget" ).css({
"color": "green",
"font-size": "16px"
})
Fact is, jQuery also has to loop through the object and assign each css style declaration, so the performance is very similar.But this is all speculation anyway. Why not read the code? A bit of searching turns up the css function: https://github.com/jquery/jquery/blob/master/src/css.js#L102 . Interestingly, it passes a callback to something called jQuery.access.
More searching finds access here: https://github.com/jquery/jquery/blob/master/src/core.js#L71... . Note line 719: If the key (which is the first argument to css) is an object, it just iterates through the object and recursively calls itself for each key-value pair in the object.
Thus, passing an object to css is equivalent to calling css multiple times, ignoring small costs of re-building the callback function that css passes to access (and I'm not even sure that cost exists, I'd actually expect any decent interpreter to optimize such that re-building it is of negligible performance cost).
The jQuery source code is a rather fun read, I highly recommend stepping through it from time to time. :)
I still prefer the latter style though, in my opinion it's far easier to read.
In my performance tests (Win 8, Chrome 25), the chained method actually performed faster than the object method. So while the idea of passing multiple arguments to a single function call (as opposed to making multiple function calls) might make sense for a general programming best practice, when applied to jQuery it fails.
My results (Chrome, Mac) are showing they have the same performance. On smaller HTML pages, multiple calls to css() actually outperformed the single call significantly (~30%).
var i, elements = document.getElementsByClassName('widget');
for (i = 0; i < elements.length; i++) {
elements[i].style.color = "green";
}
for (i = 0; i < elements.length; i++) {
elements[i].style.fontSize = "16px";
}
Two loops. The "improved" suggestion would be roughly equivalent to: var i, elements = document.getElementsByClassName('widget');
for (i = 0; i < elements.length; i++) {
elements[i].style.color = "green";
elements[i].style.fontSize = "16px";
}
This is one loop, but it's still O(n). If there are 100 ".widget" elements on the page, the first code does 200 writes to the style property. The second code does 200 writes to the style property. The difference in jumps and compares isn't worth thinking about when style changes are so expensive, the difference is probably immeasurable.The second version is better just because it's cleaner code, but that's it.
In $(".widget").css("color", "green").css("font-size", "16px"); does the color of all the widgets change, and then the fontSize, or does the color/fontSize change together for each widget? I suspect the latter...
There is no extra DOM lookups, but there will certainly be two loops.
[0] https://github.com/jquery/jquery/blob/master/src/css.js#L102
[1] https://github.com/jquery/jquery/blob/master/src/css.js#L103
[2] https://github.com/jquery/jquery/blob/master/src/core.js#L75...
[3] https://github.com/jquery/jquery/blob/master/src/core.js#L75...
http://sherlockcode.com/demos/jquery/#!/src/css.js:102
Ctrl+Click on the jQuery.access call on the next line to jump to its definition.
// Indications of Confusion
$( ".widget" ).each( function() {
$( this ).css( "color", "green" );
});
I think this is at least as likely to be the remains of a block of code that was pared down and not (yet) re-implemented differently. The developer might have intended to return with replacement code and never did, perhaps this is an ongoing project on which you're a collaborator, perhaps the developer just forgot about it. It may still be bad code, but concluding wherever you find it that the reason is fundamental ignorance seems hasty.It will always be a struggle for me to resist the bias against Other People's Code that causes me to underestimate the developer whenever I encounter something wrong. Maybe it's a minor point, but I think this bias exists in everyone and that this is an example of it.