Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkabir k
Courses Plus Student 18,036 Pointspure JavaScript vs. jQuery
What are the benefits of using pure JavaScript over jQuery, considering that you can do more with less code for the latter.
Also, under what circumstances should one use one over the other?
2 Answers
eck
43,038 PointsHi Kabir! Here is a quick comparison:
Pure JS
The most objective benefit of using pure JavaScript (or Vanilla JavaScript) is better performance. Another benefit is the improved understanding of the language you gain from writing vanilla JS, which you need if you want to be able to do more advanced JS wizardry.
The downside of vanilla JS is that it is easier to make mistakes or miss something, is less beginner friendly (than jQuery), and can require you to develop more code to accomplish goals the would take much less time with jQuery.
jQuery
The primary benefit of jQuery is the reduced development time it offers web devs and its easy to use API and numerous plugins. Also, when you use jQuery you are still writing some vanilla JS, so it is not truly one or the other.
The downside is that by adding it to your project you might bloat the resources you need to load, call unnecessarily verbose methods, and avoid learning how the language actually works.
How to choose
jQuery is a fine choice in the following situations:
- For small projects where perfomance is not a big issue
- Simple projects with a tight deadline
- Large projects that use an MVC library or framework that has jQuery as a dependency
Now, times you might not need jQuery include:
- Acquiring an intermediate - advanced understanding of JS
- Working on complex web applications
- Building you own library or framework
- Server side applications with Node.js
Here is some more on the topic.
Marcus Parsons
15,719 PointsHey Kabir,
While you are technically right that you do write less when using the jQuery library versus pure JavaScript, you have to factor in that you are loading over 82 kilobytes of data (if using minified version) just to be able to use the jQuery library. Not only that, but with pure JavaScript, the compiler can go straight to the built in function and execute. When using jQuery commands, the compiler has to find the command in the jQuery library and then execute the built in commands that jQuery uses.
Here's an interesting JavaScript performance test that tests several different operations. If you look at the first two, you can see that the first one is using the jQuery query selector and the 2nd is using the pure JS query selector. You'll see that the pure JS command is about 5x faster (at least in Chrome) to use than the jQuery command: https://jsperf.com/jquery-vs-javascript-performance-comparison/14
With all that in mind, jQuery is still a great library that has a lot to offer and with computers made in the last 10 years you won't see a noticeable difference in execution times of JavaScript and jQuery.
kabir k
Courses Plus Student 18,036 PointsThanks, Marcus.
Marcus Parsons
15,719 PointsYou're welcome, Kabir. Erik's answer has more information, though! :)
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 Points+1 Way more information than I provided :P
kabir k
Courses Plus Student 18,036 Pointskabir k
Courses Plus Student 18,036 PointsThanks, Erik. That's helpful too.
eck
43,038 Pointseck
43,038 PointsThanks Marcus! But you definitely broke down the performance concerns with much better detail :D
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsNow, if we're doing a flattery contest, prepare thyself to be blushing bright red, my good sir! haha No, but seriously, it looks like you should have the best answer on this topic. Loads of good information presented here! =]
eck
43,038 Pointseck
43,038 Pointslol, bright red blushing achieved.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsJust one more charmed soul to add to my collec-oops I'm writing this aren't I?
Michal Weizman
14,050 PointsMichal Weizman
14,050 PointsThank you Erik. I was confused about this as well, and this great answer and post solved it for me.
eck
43,038 Pointseck
43,038 PointsAwesome! I'm glad to have been helpful, Michal. ♥