I personally want a small selector function that covers the three common cases
- getElementById
- getElementsByClassName
- getElementsByTagName
It should support contexts and should not support querySelectorAll
since <opinionated>
QSA is slow as hell and should be avoided </opinionated>
I have an implementation of such a function
function select(selector, context) {
var c = selector.charAt(0),
method;
if (c === "#") {
method = "getElementById";
selector = selector.substring(1);
} else if (c === ".") {
method = "getElementsByClassName";
selector = selector.substring(1);
} else {
method = "getElementsByTagName";
}
return (context || document)[method](selector);
}
And I have a benchmark.
As can be seen it's still a factor of 4 away from native support.
Can I hand optimise this further? And if so, how?