Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting jQuery to plain old JavaScript - are the performance gains worth it?

Since jQuery is an incredibly easy and banal library, I've developed a rather complex project fairly quickly with it. The entire interface is jQuery based, and memory is cleaned regularly to maintain optimum performance. Everything works very well in Firefox, and exceptionally so in Chrome (other browsers are of no concern for me as this is not a commercial or publicly available product).

What I'm wondering now is - since pure plain old banal JavaScript is really not a complicated language to master, would it be performance enhancing to rewrite the whole thing in plain old JavaScript, and if so, how much of a boost would you expect to get from it?

If the answers prove positive enough, I'll go ahead and do it, run a benchmark and report back with the precise findings.

Edit: Thanks guys, valuable insight. The purpose was not to "re-invent the wheel" - it was just for experience and personal improvement. Just because something exists, doesn't mean you shouldn't explore it into greater detail, know how it works or try to recreate it. This is the same reason I seldom use frameworks, I would much rather use my own code and iron it out and gain massive experience doing it, than start off by using someone else's code, regardless of how ironed out it is. Anyway, I won't be doing it, thanks for saving me the effort :)

like image 942
Swader Avatar asked Dec 02 '22 02:12

Swader


2 Answers

You say the site works from "very well" to "exceptionally" - then don't bother. It won't be worth the effort, and there is no guarantee that your end result will even be more optimal than with jQuery, as the jQuery team has had years to iron out many issues.

You also say that plain old JS is "really not a complicated language" - that's not the main issue. It's not JS that's difficult to master, and what jQuery makes up for, but all the various browser quirks.

In the end, even if you do create a site that is marginally faster without jQuery, you have one that is much harder to maintain.

In short, there are times when doing such an exercise is valuable, but not when your site already works "very well", and it's not necessarily as simple as you think.

like image 176
David Tang Avatar answered Dec 22 '22 20:12

David Tang


Don't bother.

Sure you can gain some performance by using vanilla js, and in some edgecases it is worth it, but overall, as along as you use jQuery well, your gains from vanilla js are insignificant, and the crossbrowser support loss is not.

Why reinvent the wheel?

If you do wish to polish of your implementation look for places where you can store and reuse references to DOM elements, so you don't have to traverse the DOM every time you need to use them.

like image 20
Martin Jespersen Avatar answered Dec 22 '22 20:12

Martin Jespersen