jQuery element[attribute=something] vs element.class performance
-
13-09-2019 - |
Question
I am wondering which way would end up being faster, selecting elements by:
$('element[href=#my_link]');
or:
$('element.my_class');
I don't like repeating myself when I write code, so I prefer to write it the first way for the most part because then I can add information to it like:
<a href="#delete_1">Delete</a>
$('a[href^=#delete]');
and then split it up so that I have all of the information that I need once it is clicked, or whatever the action may be. Am I sacrificing overall performance because of this?
(I guess I could rewrite it as class="delete" href="#1")
Solution
- selecting an Element by class needs :
453 ms
- selecting an Element by element + search needs 578 ms
both in FireFox.
Checkout this great Webpage for more tests:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow