kyiu
kyiu

Reputation: 1976

jQuery selector performance: by ID versus by tag with ID pattern

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>?

Thx in advance!

Upvotes: 0

Views: 143

Answers (4)

A. Wolff
A. Wolff

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

Rory McCrossan
Rory McCrossan

Reputation: 337560

Selecting by id is always the fastest method as it uses javascripts own getElementById implementation.

JSPerf Demo

On my machine here (Win8, FF16.0.2) the attribute selector was 93% slower!!

Upvotes: 4

Clyde Lobo
Clyde Lobo

Reputation: 9174

The second selector will be faster as it directly maps to document.getElementById

You can check the results here

Upvotes: 1

user1796666
user1796666

Reputation:

$("#someID"); would be much faster

Because it basically uses the standart javascript document.getElementById function

Upvotes: 7

Related Questions