javascript - is document.getElementsByClassName faster than looping through the nodes? -
i working on script chrome extension have check if items exist in division. , standing before dilemma having. let's first want check classnames, tags data-ng attributes , img nested directly in tag. either using:
var first = node.getelementsbyclassname('classnameofinterest'); var second = new array(); var temp = node.getelementsbytagname('a'); for(var = 0 ; < temp.length ; a++){ if(temp[a].hasattribute('data-ng')){ second.push(temp[a]); } }temp = false; var third = new array(); temp = node.getelementsbytagname('img'); for(var = 0 ; < temp.length ; a++){ if(temp[a].parentnode.nodename == 'a'){ third.push(temp[a]); } }temp = false;
or going through elements of possible interest, , check them 3 conditions right away, this:
var nodes = node.getelementsbytagname("*"); var first = new array(); var second = first; var third = first; for(var = 0 ; < nodes.length ; a++){ if(nodes[a].classname.match(\b(classnameofinterest)\b)) {first.push(nodes[a]);} else if(nodes[a].nodename == 'a' && nodes[a].hasattribute('data-ng')) {second.push(nodes[a]);} else if(nodes[a].nodename == 'img' && nodes[a].parentnode.nodename == 'a'); {third.push(nodes[a]);} }
which of methods faster/better, , why? want make rational code ;). please help!?
Comments
Post a Comment