The first one is the fastest, simply because it has only one property to look for. Nevertheless,
document.getElementById("MyElement")
even faster. This is native javascript, and unlike jQuery, the browser immediately knows what you want to do, rather than starting to download jQuery code to find out exactly what you are looking for in the first place.
You can use jsPerf to run a speed test to compare functions: test case . Results:
$('#MyElement') Ops/sec: 967,509 92% slower $('#Mytbl #MyElement') Ops/sec: 83,837 99% slower $('#Mytbl .MyClass') Ops/sec: 49,413 100% slower document.getElementById("MyElement") Ops/sec: 10,857,632 fastest
As expected, the built-in getter is the fastest, followed by a jQuery getter with 1 selector less than 10% of its own speed. Two-parameter jQuery recipients don't even get close to the operations per second of their own code, especially the class selector, since classes usually apply to multiple elements compared to identifiers. (The original selector identifiers stop the search after they find one element, I'm not sure if jQuery is either.)
Cerbrus
source share