I currently have code that is pulling in data via jQuery and then displaying it using the each
method.
However, I was running into an issue with sorting, so I looked into using, and added, jQuery's filter
method before the sort
(which makes sense).
I'm now looking at removing the sort
, and am wondering if I should leave the filter
call as-is, or move it back into the each
.
The examples in the jQuery API documentation for filter stick with styling results, not with the output of textual content (specifically, not using each()
).
The documentation currently states that [t]he supplied selector is tested against each element [...], which makes me believe that doing a filter
and an each
would result in non-filtered elements being looped through twice, versus only once if the check was made solely in the each
loop.
Am I correct in believing that is more efficient?
EDIT: Dummy example.
So this:
// data is XML content
data = data.filter(function (a) {
return ($(this).attr('display') == true);
});
data.each(function () {
// do stuff here to output to the page
});
Versus this:
// data is XML content
data.each(function () {
if ($(this).attr('display') == true) {
// do stuff here to output to the page
}
});