Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you're already using a framework, should you never write "plain vanilla" js?

This is more of a question of style and preference than anything, though it's possible that there might be performance considerations as well.

If you're using a framework (say jQuery for the sake of argument, though it could be any framework) and you need to write a new function. It's a simple function, and you could easily accomplish it without using the framework.

Is there an advantage to using the framework anyway, because it's already loaded in the browser's memory, has a readily-accessible map of the DOM, etc.? Or will plain-vanilla js always parse faster because it's "raw" and doesn't depend on the framework?

Or is it simply a matter of taste?

like image 818
jeffedsell Avatar asked Apr 01 '11 20:04

jeffedsell


People also ask

Should I use a framework or vanilla JS?

If What You're Working on Is Simple In fact, if you're working on a smaller project, a framework will only complicate matters, so you're better off doing without. Remember, the point of using a framework is to simplify your work and save time, by implementing everything you need from vanilla JavaScript.

Is it okay to use vanilla JavaScript?

Vanilla is fine if you're building a content-heavy website. All you need is HTML, and if you have an accordion, or a slider, or a few tabs, those are all well served by the jQuery-based ecosystem. But if it has even an inkling of a web-app, you must use a framework from the outset.

Is it necessary to learning vanilla JavaScript?

If you want to become a developer who can innovate, not just execute, you need to understand the underlying principles of the web—not just the shortcuts. This means learning vanilla JavaScript before you move on to frameworks.

Does Google use vanilla JS?

No, Google certainly does not use jQuery—it is all vanilla JavaScript and (sometimes) Closure Library. As for inlining, if JS/CSS is relatively small, it is faster to inline it to minimize the number of HTTP requests. Google Page Speed Online can give you some tips on how to optimize your page.


1 Answers

The answer is going to depend greatly on what you're working to accomplish. In general, you're guaranteed at least a minor performance penalty for function overhead if you use a framework to achieve something that can be accomplished using "vanilla" JavaScript. This performance penalty is typically nominal and can be disregarded when taking other advantages of your framework into mind (speed of development, cleaner code, ease of maintenance, reusable code, etc).

If you absolutely have to have the most efficient code possible then you should try to write pure JavaScript that's highly optimized. If, like in most real world scenarios, you're not concerned about a handful of milliseconds in performance difference, stick with your Framework to maintain consistency.

like image 72
g.d.d.c Avatar answered Sep 20 '22 20:09

g.d.d.c