Which is faster than $ ("s1"). Find ("s2"). Find ("s3") or $ ("s1 s2 s3")?

jQuery is quite multifaceted when it comes to selecting specific DOM elements. Today it occurred to me that two ways to get the same elements can have different speeds:

$("selector1").find("selector2").find("selector3")

and

$("selector1 selector2 selector3")

(where selectorXcould be an identifier or a class or something else)

Both produce the same set of elements, but are there any differences in speed? How does jQuery really go through the DOM? This is especially important in the second case: does it go from selector1 to selector3 or in another way ?

Has anyone measured the difference between the two?

+5
4

$('...') , $.find() Chrome. JSPerf Benchmark , .

+4

!

console.time("Selector 1") 
    $("selector1").find("selector2").find("selelector3");
console.timeEnd("Selector 1")

console.time("Selector 2") 
    $("selector1 selector2 selector3");
console.timeEnd("Selector 2")
+2

, . $("selector1 selector2 selector3") IE, ( ) - document.getElementByClassname. IE $("selector1").find("selector2").find("selector3") , selector1 ( , ). , - - , jQuery- IE vs FF.

+1

@jcm, . , DOM .

, http://jsperf.com/complex-dom-jquery-selectors-vs-traversal-methods

jQuery (v1.6). , .find() , . . jQuery ( ) , (, , find). !

, , , , , , , .

0

All Articles