Thursday, 21 October 2010

Optimising jQuery each

Yea, another optimisation post, it's not an obsession, it's just this is pretty interesting. Jamie Stewart tweeted about this article on optimising jQuery each. I really like the idea of creating an empty jQuery object and passing context to it - it just seems smart. I'm always wary of optimising prematurely though so I wrote a little speed test page of my own. You can run the tests yourself, and then come back and comment :).

The results of which seem to show to me that whilst the optimisation is faster, it's probably not going to be noticeably faster in my day to day usage. Still, faster is faster, right? Well, not always, I would see using this as a standard replacement for .each as a premature optimisation. Whilst it is faster, it's not no-brainer faster; there are other factors to consider:
  • The optimisation comes in plugin form, which is extra code to download and possibly an additional http request

  • Breaking from jQuery core means a possible break from future jQuery optimisations

  • Faster, IMHO in this case, doesn't trump readability
I would definitely consider this method for large iterations and when I know something is slow, but it won't become my go to replacement for .each - despite how cool an idea it is.

No comments:

Post a Comment