Here is the speed hierarchy in order of fastest to slowest
ID : $(‘#myElementID’)
Element : $(‘div’)
Class : $(‘.myClassName’)
To find a bare class name, as in #3 above, jQuery goes through every element in the entire DOM. To find an element, on the other hand, it uses getElementsByTagName(), thereby limiting the number of elements it has to traverse right from the start. Therefore, if we know which element the class is tied to, we can speed up the traversal significantly by referring to it, using, for example, $(‘div.some-class’) rather than $(‘.some-class’).
Likewise, we can help jQuery gather our classes faster if we can limit where jQuery looks to a particular ID: $(‘#sidebar .menu’), not $(‘.menu’).