Reputation: 1976
I'm pretty new to using jQuery and consequently I get along well enough to get the expected result but here's a question regarding performance, to which I'm not sure about the answer:
Which of the following selector is the most performant, assuming that the expected selection actually is a <textarea>
?
$("textarea[id='someID']");
$("#someID");
Thx in advance!
Upvotes: 0
Views: 143
Reputation: 74420
For the fastest result, you have to wrap js getElementById in jquery object:
$(document.getElementById('myid'));
See http://jsperf.com/id-selector-vs-attribute-selector/3
Upvotes: 0
Reputation: 337560
Selecting by id
is always the fastest method as it uses javascripts own getElementById
implementation.
On my machine here (Win8, FF16.0.2) the attribute selector was 93% slower!!
Upvotes: 4
Reputation: 9174
The second selector will be faster as it directly maps to document.getElementById
You can check the results here
Upvotes: 1
Reputation:
$("#someID");
would be much faster
Because it basically uses the standart javascript document.getElementById
function
Upvotes: 7