What is better from performance wise document.getElementById('elementId')
or $('#elementId')
? How can I calculate the speed by myself?
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.
ID and Element selector are the fastest selectors in jQuery.
getElementById is very fast and you shouldn't have to worry about performance.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With