Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
document.getElementById("abc")

or
$("#abc");


Which will work faster and Why?
I tried to search a lot but did not find anything convincing.Please help!
Thanks in advance.
Posted
Updated 16-Jul-19 18:59pm

By common sense, since JQuery is written in Javascript, it should be slower. Of course that doesn't mean 'noticeably' slower.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jan-16 10:32am    
5ed.
—SA
CPallini 12-Jan-16 10:48am    
Thank you.
Patrice T 12-Jan-16 22:57pm    
+5
CPallini 13-Jan-16 3:37am    
Thank you.
Why not just test it yourself?

HTML
<div id="a">
    <div id="b">
        <div id="c">
            Hello
        </div>
    </div>
</div>

<div id="resulta"></div>
<div id="resultb"></div>

<script type="text/javascript">
    var start = new Date().getTime();

    for (var i=0; i < 1000000; i++)
    {
        var x = document.getElementById('c');
    }
    var end = new Date().getTime() - start;

    document.getElementById('resulta').innerHTML = 'Non jQuery: ' + end;

    start = new Date().getTime();

    for (var i = 0; i < 1000000; i++) {
        var x = $('#c');
    }
    var end = new Date().getTime() - start;

    document.getElementById('resultb').innerHTML = 'jQuery: ' + end;

</script>


jQuery is substantially slower, which of course it would be. jQuery isn't magic, it's not a new language supported by the browser, it's simply a bunch of javascript functions. As it offers more functionality than the basic getElementById it is running more code, so is slower. The $() function is not just finding the element but creating a jQuery object based on the element it has found.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jan-16 10:33am    
5ed.
—SA
We cannot perceive the concept of "slower" and "faster" by just one execution. That is because you cannot measure milliseconds yourself. You would use CPU for that. Set a timer, execute an instruction (at least!) 10,000 times and then calculate the time taken by one execution.

jQuery is slower because it is written in JavaScript and has a notably extra topping to make sure the code looks smaller and concise. The code that you wrote,
JavaScript
$('#abc');

Always calls the JavaScript function,
JavaScript
document.getElementById('abc');

But, it will not directly transition down to that document call! It will first of all determine whether to look for an element with its ID ('#') or its class name ('.'). Many other patterns are used that bring down this line to that one. Now, personally, I don't find jQuery any better than JavaScript if all you have to do is, select an element in HTML DOM.

For me, using jQuery would only make sense if you are performing animations, executing simultaneous Ajax requests and so on. Another "good" use of jQuery would be to use it in UI, jQuery UI[^] for instance.

Please see Vanilla JS[^] website for more ideas about the difference. They have a good table denoting a common different between many frameworks and JavaScript itself.

Also, as mentioned already, you cannot find the difference by just executing one command. Try executing a command 10,000 times and then see the difference. Then you can take out the time taken by one execution.

Your question is more like, "Why is C slower than Assembly language?" :-)
 
Share this answer
 
v3
Comments
[no name] 12-Jan-16 9:50am    
Great Detail Information. Have a 5.
Afzaal Ahmad Zeeshan 12-Jan-16 10:39am    
Thank you.
Sergey Alexandrovich Kryukov 12-Jan-16 10:38am    
5ed.
I would change the phrase with "compiles down". Compiles what? In this case, the expression in first line simply calls document.getElementById. However, "compilation vs interpretation" issue is not so simple in JavaScript. It's not primitive 100% interpretation. At least, the whole script is checked for lexical integrity at first. It can be easily detected. If several statements on top are correct, they won't be interpreted at all if below, say, brackets are unbalanced...
—SA
Afzaal Ahmad Zeeshan 12-Jan-16 10:45am    
Thank you, Sergey.

You're right, it is much interpreted than it is compiled (if any!). Living in C and C++ realm made me forget the terminology in different languages, so it is my bad. :-) Changed it to "translated down", hoping it may suffice.
Sergey Alexandrovich Kryukov 12-Jan-16 10:46am    
I would simply use more neutral term "calls"...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900