question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Selecting elements with a list of IDs is orders of magnitude slower than selecting them individually

See original GitHub issue

Description

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:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dmethvincommented, Sep 12, 2016

It sounds like this page is into a super edgy territory if it has hundreds of thousands of elements.

$('#firstID,#secondID')

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.

$([document.getElementById('firstID'), document.getElementById('firstID')]);

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.

1reaction
mgolcommented, Sep 12, 2016

The thing is $('#firstID,#secondID') is not equivalent to $([document.getElementById('firstID'), document.getElementById('firstID')]), it runs via querySelectorAll. 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:

$('#firstID').add('#secondID');
Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found