Reputation: 593
for (i = 0; i < $('body > p font i').length; i++) {
current = [$('body > p font i').eq(index), $('body > p font i').eq(index).index('body > p font u, body > p font i')];
getState(current[1]);
}
function getState(index) {
// Lookup the object's index, then crawl up until you find a match
while ($('body > p font u, body > p font i').eq(--index).filter('u').length == 0);
console.log($('body > p font u, body > p font i').eq(index).text());
}
Fairly simple question. I'm iterating a jQuery result set against a selector filter until I find a match, climbing up through the result set as I go.
The longer this loop runs, the slower it becomes, almost exponentially so.
Upvotes: 4
Views: 134
Reputation: 4504
You are seraching in the DOM tree on each iteration which is an expensive operation, the solution is cache:
var nodes = $('body > p font i');
for (var i = 0, size = nodes.length; i < size; i++) {
current = [nodes.eq(index),nodes.eq(index).index('body > p font u, body > p font i')];
}
Upvotes: 2