When `this` is already a DOM element, `this.id` is the direct way to read its ID. `$(this).attr(“id”)` goes through a jQuery wrapper and method call, so it adds work; however, the two expressions differ when the element has no ID, and old timing results are not a reliable speed ratio for current browsers.
What each expression reads
In a jQuery event handler or iteration callback, `this` commonly refers to the current DOM element. In that context, `this.id` reads the element’s native `id` property.
`$(this)` wraps that element in a jQuery object, and `.attr(“id”)` asks jQuery for the value of its `id` attribute. jQuery documents `.attr(name)` as getting the attribute value from the first element in the matched set. See the jQuery .attr() API documentation.
How the results differ when an ID is missing
For an element with a set ID, both expressions ordinarily give you that ID as a string. If the element has no `id` attribute, they do not return the same value: the DOM `id` property is an empty string, while jQuery documents `.attr(“id”)` as returning `undefined` for an unset attribute (since jQuery 1.6).
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
That distinction matters in conditions and defaults. For example, an empty string and `undefined` are both falsy in JavaScript, but strict comparisons, nullish/default handling, or code that checks the returned value explicitly can behave differently.
What the published speed tests show—and what they do not
Sam Deering’s SitePoint article, published April 12, 2013 and updated February 13, 2024, reports a quick test of 42 ms for `$(this).attr(“id”)` and 1 ms for `this.id`. The result illustrates the expected direction: direct property access avoids the jQuery wrapper-and-method path. It is not a controlled cross-browser study or evidence that `this.id` is universally exactly 42 times faster. The article does not establish enough about browser, hardware, jQuery version, or workload to apply that ratio to a current project. Read the SitePoint comparison.
Rank #2
- JavaScript Jquery
- Introduces core programming concepts in JavaScript and jQuery
- Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
A separate historical test by Growing with the Web, published October 17, 2012, timed one million operations. The author reported these results in milliseconds:
| Expression | Reported times for one million operations |
|---|---|
div.attr('id') |
1,545 / 1,536 / 1,558 ms |
div[0].id |
48 / 46 / 45 ms |
div.get(0).id |
59 / 69 / 62 ms |
div[0].getAttribute('id') |
74 / 72 / 74 ms |
These are that author’s environment-specific historical measurements, not present-day browser benchmarks. They reinforce that access paths can have different costs, but do not predict a particular application’s performance. See the Growing with the Web test.
Free tools Windows power users keep installed
One-click scans. No signup required.
Which form should you use?
- Use `this.id` when `this` is the DOM element and you need its ID property. It is the straightforward choice and avoids invoking jQuery just to read one property.
- Use `$(this).attr(“id”)` when the surrounding code already needs a jQuery object or specifically needs jQuery’s attribute-getter behavior.
- Benchmark the real workload if this access is on a performance-critical path. Measure in the target browser and application rather than treating a historical microbenchmark as a guaranteed multiplier.
Keep IDs unique in the document
Whichever access form you choose, an ID should identify only one element in a document. jQuery’s ID selector documentation notes that IDs should be unique; duplicate IDs make ID-based selection unreliable. This does not change the property-versus-attribute distinction, but it is worth checking if the code uses an ID to find elements. See the jQuery ID Selector documentation.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

