Selecting elements with a list of IDs is orders of magnitude slower than selecting them individually
See original GitHub issueDescription
There isn’t a significant benefit to using $(document.getElementById('firstID'))
over $('#firstID')
, but using $('#firstID,#secondID')
is several thousand times slower than $([document.getElementById('firstID'), document.getElementById('firstID')]);
This affects all the browsers I’ve tested: Chrome, Firefox, IE11, Edge
Link to test case
https://www.measurethat.net/Benchmarks/Show/129/3/element-by-id-fetching-javascript-vs-selectors See the final test case ‘2 divs with single jquery selector’.
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Chapter 4. Query Performance Optimization - O'Reilly
mysql> SELECT actor_id, COUNT(*) FROM sakila.film_actor GROUP BY actor_id;. This query returns only 200 rows, but it needs to read thousands of rows ......
Read more >mysql - What is faster, one big query or many small queries?
The queries themselves can be more efficient. In this example, using an IN() list instead of a join lets MySQL sort row IDs...
Read more >Which CSS selectors or rules can significantly affect front-end ...
So when a very basic engine sees that (and since this is the first rule), it goes and looks at every element in...
Read more >Speeding up SQL queries by orders of magnitude using UNION
The Union simply calcualtes them separately, rather than doing ... With a join SELECT * FROM A INNER JOIN B ON A.id =...
Read more >How to design SQL queries with better performance: SELECT ...
When writing queries, it would be better to set the columns you need in the select statement rather than SELECT *.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It sounds like this page is into a super edgy territory if it has hundreds of thousands of elements.
This creates a jQuery set that guarantees the elements are in document order. If
#secondID
comes before#firstID
in the document, they will appear in that order in the collection. The sort operation might be expensive.This creates a jQuery set out of whatever you give it with very little verification and no sorting. (They might not even be DOM elements.) Cheap, but it provides no guarantees about the set.
Edit: For example, your set there has the same ID twice! Normally that would be de-duped but not when you provide an array like that.
The thing is
$('#firstID,#secondID')
is not equivalent to$([document.getElementById('firstID'), document.getElementById('firstID')])
, it runs viaquerySelectorAll
. The non-trivial part is that you’re passing a single string with a comma; we’d need to tokenize it first which is non-trivial work. This will always be slower.If you want to have an equivalent of running
getElementById
twice, you’ll have to write: