Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance using JS querySelector [closed]

When using JavaScript in the web browser is there any performance difference between the following:

Existing getElementById

document.getElementById("elem"); 

Query Selector using #id

document.querySelector("#elem"); 

Query Selector using [id=elem]

document.querySelector("[id=elem]"); 

I'm assuming the first one will be fastest (only has to lookup elements with an ID). Also the final one looks like bad practice. I like the second one as using querySelector for everything makes the code easy to read.

Any suggestions?

like image 825
Rob Farr Avatar asked Feb 23 '13 22:02

Rob Farr


People also ask

Is querySelector faster?

querySelectorAll is much faster than getElementsByTagName and getElementsByClassName when accessing a member of the collection because of the differences in how live and non-live collections are stored.

Which is faster getElementById or querySelector?

Note getElementsByClassName returns a live node list, whereas querySelector(All) and getElementById don't. getElementById is faster (but only noticeable in performance tests) because since very early Internet-Explorer days the Browser creates global variables for all ID values.

Should I use getElementById or querySelector?

The getElementById method retrieves an element by its DOM ID. Both methods have wide browser support. You should opt to use the querySelector method if you need to select elements using more complex rules that are easily represented using a CSS selector.

Why querySelectorAll not working?

Originally Answered: Why is my querySelectorAll() method not working ? The reason is that you try to access DOM elements before they are part of the DOM. There is no reason to use the querySelector at all unless you have a table already you want to modify.


2 Answers

Firstly,

document.querySelector("#elem"); 

Has an advantage in the fact that, unlike document.getElementId, it can return classes. However, the usefulness of this is far diminished by the fact that it only returns the first object with that class name, so you might as well just use an id if you're not specifically looking for the first object with that classname. if you use,

document.querySelectorAll 

However, I believe (I may be wrong), it returns all items with that classname as an array, where regular querySelector is equivalent to querySelectorAll[0]. One more advantage, is that you can run css3 queries through it, which can be quite useful.

Secondly,

document.getElementById("elem"); 

Has a very good advantage over queryselector in the sense that it is almost 5 times faster, so if you're sitting there with several thousand lines of code and you want to optimise said code, then getElementById is the way to go.

Lastly,

document.querySelector("[id=elem]"); 

I, personally, don't see the need to use this in any situation. If you needed a querySelector, why not just use a # ? This is exactly equivalent to your first example of querySelector, however it has a lot of useless charaters.

Edit: Just to be clear, in summary, you're probably better off using document.getElementById.

like image 197
Bernie Avatar answered Sep 23 '22 02:09

Bernie


You can test it yourself. getElementById is a fastest method

like image 41
Silver_Clash Avatar answered Sep 21 '22 02:09

Silver_Clash