my work requires me to work with jquery a lot. Since i am always trying to find a better way to do things, i would like to improve performance on javascript level of page.
so i was wondering what is the fastest way to get specific children of DOM element in Jquery?
Lets say we have it like this:
parent
|-element 1
|-element 2
|-parent element 1
|---element A
|---element B
|-parent element 2
|---element C
|-element 3
Which of the following methods would be most suitable to get every specific element, assuming every element in the first branch of dom tree has an identifiable id or class different from others and there is no limitations to what kind of elements they are?
Method 1:
getting each element with its own selector as such:
var element1 = $(".element1");
var element2 = $(".element2");
...
Method 2:
getting each element with parent element as such:
var parent = $(".parent");
var element1 = parent.children('.element1');
var element2 = parent.children('.element2');
...
Method 3:
getting the whole thing in array and parsing trough it as such:
var elements = $(".parent").children();
var element1 = elements[0];
var element2 = elements[1];
var elementA = elements[2].children('elementA');
// or maybe like this?
// var elementA = elements[2].children()[0];
...
On the dom tree presented i also included nested parents and children. Which method is most suitable to get those, especially if they do not have a unique identifier?
for instance with regard to dom tree presented: would it be faster to get element like this (assuming parent element has no unique identifier):
var elementA = $(".parent").children(".parent-element-1").children(".elementA")
or
var elementA = $(".parent").find('.elementA')
?
Also if there is any other way (except calling elements through sizzle) to get to elements, please do tell me about it :)
I am aware i could call elements via javascript alone, but with a few hundred dom elements, such a task would probably be way outside my usual time frame - or is this standard?
EDIT:
sizzle is slow: http://jsperf.com/sizzle-is-slow/2
What im asking about is the proper use of jquery/javascript methods.