Should I be worried about JavaScript speed when using jQuery?

I am writing jQuery for a page that is a complex jumble of many DOM elements (I have no control over this). The main target browser is IE7 and I'm worried about the speed of its JavaScript engine. The scripts I've already written are laggy and set up as efficiently as possible.

I could write a script that is simple to maintain and understand, for example using one contains

. Or I can help the engine by narrowing down the DOM search through this messy HTML, which leads to more complexity.

Should I care about the browser's JavaScript engine speed when writing jQuery scripts?

+2


source to share


8 answers


jQuery is written in JavaScript. Therefore, you should keep an eye on the speed of the JavaScript engine, as this also affects the speed of your jQuery code.



There are many tips and tricks on how to write more efficient jQuery code. The basic principle is to understand how jQuery works, how selectors translate into queries that select elements from the DOM, etc.

+5


source


Try using native Javascript methods instead of jQuery methods whenever possible and you will generally see significant speed improvements , especially in IE. IE JS rendering engine is SLLLOOOOWWW compared to other incompatible browsers.



For example, I had an AJAX script that worked fine in FF / Safari, but worked like frozen molasses in IE. It turns out that the developer who wrote heavily used the jQuery $ .each () method to get data from a large JSON file. I rewrote the script to use standard JS outlines for () instead, and after doing that, several speed tests on it showed that using for () was more than twice as fast.

+2


source


Yes, but you shouldn't dwell on this :)

0


source


I haven't come across a single instance where the benefits of using jQuery or a plugin (cross-browser compatibility, extremely useful features) are outweighed by the speed reduction. And sometimes I work with pretty large web pages.

Either way, the only way you're going to know is to try it. Write your code in the smartest way first. If it's too slow, you can optimize it.

Optimizing DOM Traversal
http://www.learningjquery.com/2006/12/quick-tip-optimizing-dom-traversal

0


source


Contains can be relatively slow as I don't believe the document model is indexed in any way by what text appears inside the elements.

That said, I'm not sure if it's wise to try to overestimate something and optimize for the sake of optimization. Go for the simplest thing that works, tests and then optimizes if you have a problem.

0


source


No, you shouldn't care about this, because it shouldn't affect how you write your code.

That is, you should ALWAYS write effective Javascript because it is just good practice and because for most sites you have no idea which browser any particular user is using, so you can assume the worst as well.

0


source


Yes, you should be concerned, but luckily the jQuery team is just as concerned:

Recent changes to jQuery Internals (Check last two slides)

0


source


Don't optimize, but optimize as much as you need.

If it's slow then you really have no choice but to go there and help jquery. But if it isn't, then why bother.

0


source







All Articles