Ok… I have the following setup:
A div with n divs as children (n being quite big). I need to hide/remove (doesn’t really matter – i think remove is better though, if you have an opinion here is helpful) the 3rd, 4th, 5th, 6th, 9th, 10th, 11th, 12, 15, 16, etc. (n+1 modulo 6 > 1 – 0 and 1 will not be hidden).
What is faster:
Get the parent div children and use the logic to hide divs:
childrenArray = document.getElementById('id').childNodes;
lng = childrenArray.length;
for (i = 0; i < lng; i++)
if (i % 6 > 1)
// Remove here.
Or add a class to desired nodes and check for it. jQuery class selector (or a hand written script which would be actually faster) and then remove.
Note: Before you say to you jQuery class selector is better consider that the array is still checked.
So basically what is faster check an element property or do a mathematical operation and a comparison?
Not pretty sure but I think the first option will be faster, because if you add a class attribute to an element then the jquery selector will have to look into the whole document because the more than one element can have same class, so it has to traverse the whole document.
But in the first case if you find the desired dive you don’t need to look into the whole document. This way it will be faster.
Also you can use id attribute to select the desired element.