Andrew Ingram lives in London, England. He likes design, photography, coding and being silly.

Be Careful with jQuery

Note: This article was written for a previous version of the site and is likely out-of-date.

Let’s get this out of the way first; I love jQuery, it makes DOM(Document Object Model) manipulation straightforward and has just the right level of involvement for a JavaScript framework. Basically if you’re doing nothing particularly fancy, you need nothing more than the jQuery Core and you could reasonably find yourself using most parts of the core functionality without being guilty of going a bit too crazy with JavaScript functionality.

The jQuery selectors are fast, and they seem to keep getting faster. It’s got to the point where using a jQuery selector is only marginally slower than using the native DOM functions.

The problem is with chaining. As one of jQuery’s most praised features chaining is a mixed blessing, you can do a lot of powerful things in just one line using it but chances are you’ve solved the problem in a way that’s far from efficient. Each time you add another link to the chain you are effectively adding another loop that must be iterated over; in just a few short steps you will probably have added a lot of algorithmic complexity and unnecessary repetition to your code.

There are a few tricks you can use to optimise your code (this applies to any coding, but I’ve found jQuery makes it a little bit too easy to be lazy):

Feel free to contribute any other tricks you've come across that improve the performance of jQuery-based algorithms

Originally published on .