
Today I needed to select all the h3's within a div and set a margin-top property on all items in the returned set except the first.
I came up with a few ways to return the results I was looking for (here are some
jQuery examples):
$("#div h3").slice(1);
$("#div h3:not(:first)");
$("#div h3:gt(0)");I've been reading a lot about
how your selection can be optimized based on how you structure your query. So I ran each query through Firebug Profile to see which selector query was the fastest. Load the page, click on the "Profile" button at the top, run your query in the console, click the "Profile" button again to stop the profile. You'll get the time it took to execute that statement. Here are the results for each:
$("#div h3").slice(1); 3.305ms
$("#div h3:not(:first)"); 0.705ms
$("#div h3:gt(0)"); 2.347msIt's clear from my testing that the second query is 2-3 times faster then the rest for my page and my specific uses. Your specific profile times may vary based on HTML structure and selector query. So it's always good practice to test the speed of your selector if there is more then one way to get your results so that you are using the most efficient selector for your situation.