I was reading a thread and saw this post, which says that Javascript is faster than Jquery because it uses native code. But why is the use of native code faster? Is this always the case? What impediments does Jquery have in performance?
-
What that post actually says is that getElementById is faster, because it uses native code. The jQuery selector will also use getElementById, but it first needs to do a lot of tests and comparisons on the text. You have conflated that to mean that javascript is faster than jQuery, but it doesn't mean that at all.– Robert HarveyCommented Dec 18, 2013 at 23:42
2 Answers
First of all, we have to get something straight:
Javascript and jQuery are not two completely different things: jQuery is a Javascript library.
jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML.
JavaScript (JS) is an interpreted computer programming language. As part of web browsers, implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed.
That said, the common trend among programming languages and libraries is that the library is always going to be slower because it is more code to run alongside your actual program.
For more information, check out this post. It's pretty similar to what you're asking.
The link you posted is not that JavaScript is faster per se, but rather that calling the native DOM method is faster than calling a JavaScript abstraction that eventually calls the same DOM method.
For tasks that jQuery does well, such as gathering a list of DOM elements based on a class name, it would be difficult to write a "pure" JavaScript function that does the same task measurably faster. (There are the occasional browser-specific optimization... but jQuery already includes several.)
-
document.getElementsByClassName is a "pure javascript function" which does exactly that without all the overhead.– SamCommented Sep 8, 2017 at 12:45