1

I was wondering how jquery really works internally. For example I want to know how jquery selectors works under the hood ? Does it loop through all DOM elements ? Or it has another magic algorithm for finding elements ?

And how does it differs from .find() and .filter() functions ?

Do you guys have any idea ?

Pouyan
  • 2,849
  • 8
  • 33
  • 39

1 Answers1

0

jQuery uses pretty much the same principles as browsers themselves when they need to apply style rules from elements - scan all elements and check against all rules declared in CSS.

And so does jquery:

for each element in given subtree/input set do
  if element matches selector do 
     add element to output set
  end if
end for
return output set

So the task has computational complexity of O(N) where N is a number of DOM elements in input set.

There are quite few tricks/optimizations possible but in general it is as complex as that.

Some jQuery selectors have even worse complexity, for example this $( "li:has(ul)") selector has computation complexity of O(N*N) so use with care.

c-smile
  • 26,734
  • 7
  • 59
  • 86