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 rath开发者_JAVA百科er 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 :)
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.
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.
No.
I'm sorry, but unless you are as skilled as the jQuery developers, this might not be a good idea.
Yes, you might cut out some code that does not get used, and you might maybe even gain a bit of performance. But if you are not as skilled, you'll probably end up missing a lot of improvements the team implemented and won't even gain speed.
The biggest reason why people prefer jQuery over JavaScript is the speed of development, the maintainability and the cross-browser interoperability. You risk missing out on all that.
- Higher-level code means faster development, but also faster support in the future. Finding and fixing a bug in high-level code is easier.
- The jQuery code will be updated by the team when new issues are found or new browsers arrive. You will have to duplicate all these changes in your own code.
- Should you encounter a bug in your own code, you're on your own. If you encounter difficulties with jQuery scripts, there is an enormous userbase that can help you.
- And don't get me started on all the plugins that exist. What if tomorrow your client wants you to add widget X to your site, and you don't have jQuery? Will you re-invent that plugin too?
The root of all evil.
精彩评论