Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery, performance-wise what is faster getElementById or jquery selector?

What is better from performance wise document.getElementById('elementId') or $('#elementId') ? How can I calculate the speed by myself?

like image 792
Ahmed Magdy Avatar asked Dec 06 '09 09:12

Ahmed Magdy


People also ask

Is document getElementByID faster than jQuery?

The document. getElementbyId( "myId") is faster because its direct call to JavaScript engine. jQuery is a wrapper that normalizes DOM manipulation in a way that works consistently in every major browser.

Which jQuery selector is fastest?

ID and Element selector are the fastest selectors in jQuery.

Is getElementByID fast?

getElementById is very fast and you shouldn't have to worry about performance.

Which is fast document getElementByID (' txtName ') or #txtName ')?

The simple answer is document. getElementByID('txtName') is faster than $('#txtName') because jQuery is written on top of javascript. It means internally jQuery is using only native javascript codes.


1 Answers

If you are concerned about performance, native getElementById is much much faster, but jQuery gives you very handy access to a lot of stuff. So, you might want to use something like :

$( document.getElementById("some_id") ).jQueryCall(); 

This will give you a better performance, and at the same time, allow you to use jQuery.

like image 145
jeffreyveon Avatar answered Sep 24 '22 04:09

jeffreyveon