When searching for an id
, the selector will halt as soon as it's found a match (even if there are many) - I assume there's some sort of key/value lookup table for this purpose, as it's much faster than DOM traversal. Here's why, and here's an excerpt:
It's still much
better to select by ID...because
jQuery uses the browser's native
method (getElementByID) to do this and
doesn't have to do any of it's own DOM
traversal, which is much faster.
The linked results there show >100x speed improvement with id
vs class
.
When searching for a class
, the entire DOM (or scope) is searched. Here's a benchmark using scope.
You can benchmark selectors in your own browser here.