Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  8] [ 3]  / answers: 1 / hits: 6000  / 11 Years ago, mon, january 27, 2014, 12:00:00

I have a scenario in which I know that the div I'm looking for is exactly two levels deep.



Is it more efficient to use:



$('#mydiv').find('.myselector')


or



$('#mydiv').children().children('.myselector')

More From » jquery

 Answers
12

Use your console to check. First check your first suggestion:



console.time('benchmark');
for (var i=0; i<1000; i++) {
var $elem = $('#mydiv').find('.myselector');
}
console.timeEnd('benchmark');


Now do the same for your second suggestion:



console.time('benchmark');
for (var i=0; i<1000; i++) {
var $elem = $('#mydiv').children().children('.myselector');
}
console.timeEnd('benchmark');


Run both versions a few times to really check if there is a significant difference. Use this way of testing to optimise your selectors.


[#48300] Monday, January 27, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
julian

Total Points: 159
Total Questions: 105
Total Answers: 94

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;