News for hackers. Stuff that matters.
Re:JQuery implicit loops by m0th87 on Friday February 01, 2013 @11:06AM (#5151498)
The performance implications section is completely wrong. jQuery doesn't re-query for elements on each call; the results are cached. Here's a test: http://jsperf.com/jquery-s-css-object-vs-multiple-calls

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%).

Re:JQuery implicit loops by elijahmanor on Friday February 01, 2013 @11:14AM (#5151553)
You are correct in that jQuery doesn't re-query the DOM for elements on each call, but what it does do is re-loop through it's internal cached collection to apply these CSS rules. The perf hit isn't noticeable when jQuery's selection (internal collection) is small. Imagine a webpage with lots of elements you want to select and then want to interact with... that is what I am trying to bring attention to.
Re:JQuery implicit loops by m0th87 on Friday February 01, 2013 @11:29AM (#5151673)
What's your definition of a non-small jQuery selection? That sample test loops over 272 elements with no discernible performance effect.
Re:JQuery implicit loops by pixl8ed on Friday February 01, 2013 @11:58AM (#5151863)
Is it re-looping? Doesn't the .css setter return a jQuery object that is used by the next method in the chain? So it's actually only going through an implicit loop of $( ".widget" ) once?
Re:JQuery implicit loops by evan_ on Friday February 01, 2013 @12:38PM (#5152123)
It's looping twice, because the first time you call to .css it loops through each element to add the first style, and then it loops through again on the second .css to add the second style. You could roughly replicate it with:

    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.

Re:JQuery implicit loops by pixl8ed on Friday February 01, 2013 @01:32PM (#5152455)
I'm not convinced that there's two loops -- aren't the .css() methods chained together, and would be executed right after each other for each step through the implied loop?

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...

Re:JQuery implicit loops by thezilch on Friday February 01, 2013 @01:59PM (#5152614)
Chained functions call independently and completely before returning to the next. The css function [0] operates on the "array" this returned by $ using an anonymous function [1] at a call site in the access function [1] [2]; access then returns the object formerly known as this [3]. Chain, rinse, and repeat to your heart's content.

[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...

Re:JQuery implicit loops by pixl8ed on Friday February 01, 2013 @02:42PM (#5152936)
Doh -- of course... Many thanks for the explanation & the links!
Re:JQuery implicit loops by sim0n on Friday February 01, 2013 @01:54PM (#5152580)
There must obviously be two loops, jQuery can't perform magic. The first .css() call will iterate over the cached array of elements, apply the color attributes and then return a reference to the jQuery instance. The second call will loop once again and this time apply the font-size attribute.

There is no extra DOM lookups, but there will certainly be two loops.

Re:JQuery implicit loops by dugmartin on Friday February 01, 2013 @02:16PM (#5152745)
You can walk through what the .css function call is doing here:

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.

Re:JQuery implicit loops by davej on Friday February 01, 2013 @12:46PM (#5152168)
Also, I'd imagine you could get hit with lots of UI repaints that could be minimized if you iterated over the loop manually.