Tuesday, July 20, 2010

Quick Tip – Optimizing DOM Traversal » Learning jQuery - Tips, Techniques, Tutorials

The topic of optimization has come up a number of times in the jQuery mailing list. jQuery’s DOM traversal is powerful and easy, but it can sometimes be slow as well. As with any JavaScript library or framework, the helper methods will be slower than the plain old JavaScript (p.o.j) methods. Nevertheless, if we keep in mind the jQuery’s speed hierarchy, the speed difference between jQuery and p.o.j. will often be negligible.

The “speed hierarchy” goes like this, in order of fastest to slowest:

  1. ID : $(‘#some-id’)
  2. Element : $(‘div’)
  3. Class : $(‘.some-class’)

jQuery uses JavaScript’s native getElementById() to find an ID in the document. It’s fast — probably the single fastest way to find something in the DOM — because it’s only looking for a single unique thing, and it’ll stop looking once it has found it. So, if we have <h2 id="title">This is my title</h2>, we would use $('#title'), not $('h2#title') to access it.

To find a bare class name, as in #3 above, jQuery goes through every element in the entire DOM. To find an element, on the other hand, it uses getElementsByTagName(), thereby limiting the number of elements it has to traverse right from the start. Therefore, if we know which element the class is tied to, we can speed up the traversal significantly by referring to it, using, for example, $('div.some-class') rather than $('.some-class').

Likewise, we can help jQuery gather our classes faster if we can limit where jQuery looks to a particular ID: $('#sidebar .menu'), not $('.menu').

I’m sure there other ways to make DOM traversal speedier, so if anyone knows any other tricks, I’d love to see them in the comments. Also, I should probably reiterate that if speed is all you care about, no library/framework is going to beat p.o.j.

Update: Motivated by enej’s comment, I put together a little speed-test page where you can try out a number of different DOM queries and see how fast they are. Since I posted a link to the page on the jQuery mailing list, there has been quite a bit of activity in this area. Aaron Heimlich extended the speed test to work in conjunction with Firebug for some awesome reporting, and rumor has it that Yehuda Katz is working on an entire speed-test suite.

Update

Please be aware that these optimizations are not necessarily helpful as of jQuery 1.3 because of the new “Sizzle” selector engine.

A handy tip for those who are interested in speeding up their jQuery code. It may seem obvious, but I've had a conversation about this in the last couple of days, so thought it would be handy to post!

Posted via email from Mark Kennard's Posterous

An open letter to Harvester Restaurants

To Whom It May Concern.

I visited your Harvester in Fareham last night as a family gathering to celebrate my girlfriend's birthday, and frankly, it was probably the worst meal of my life, after a catalogue of issues from the moment we arrived. I shall list each point for you so you can see just how badly your Fareham branch is run.

1) When we arrived, we were greeted by a barman who was surly at best. There was no "good evening, what can I get you", and when I want to pay by card, he just said "I havent got a card reader" in a merable surly fashion, and walked off, then returned and just held the reader out for me to use. Maybe, as a front of house member of staff, he should be a little more polite and maybe even try to engage customers as they arrive.

2) When we got to our table, despite the staff seeing we had two children with us, we had to wait nearly over 10 minutes for a child's menu

3) When I went to the salad bar, there were no white rolls, no potatoe salad left, no pasta salad left, and the coleslaw was made with lettuce!! The lettuce all looked dry and tired, the peppers looked slimy and frankly, I was unwilling to eat any of it.

4) When our order eventually arrived, the waitress didnt clear away all the used salad bowls and side plates, which we had stacked at the end of the table when we finished them.

5) None of the plates of food had any sort of garnish, just a plate of tired, brown looking food. My chips were cold, and the extra prawns my girlfirend ordered to accompany her fillet steak looked revolting, pale and dry, and to add insult to injury they were cold. I asked the waitress to get me some hot chips and sent the prawns back too, but when she returned, the prawns still looked inedble and my chips were still not hot!

6) Not wanting to spoil my girlfriends meal, I decided not to cause a scene or complain until the end of the meal, but when the waitress returned with our drinks order, she then spilt a drink on my meal, and then laughed!!! Maybe if the bowls from the salads had been cleared, she would have had room to put the drinks down safely. At this point, I had to go outside and have a cigarette as I was so angry at the complete ineptitude of your entire operation and how it was spoiling my girlfriends evening out.

7) At this point I complained to one of the waiting staff, who said she had told the manager about the situation, and the managers response to her was "Leave them til they have finished their meal". Surely, a better approach would be to come over and see if she could help sort things out and ensure our evening wasnt completely ruined. No such luck.

8) Finally, when the manager did eventually come over and speak with us, she just said "I understand you havent had a very good meal". I then outlined the catalog of failure to here, and she didnt even say sorry. She just said ok, we will only charge for the drinks. Then she left us. She didnt return to the table, but instead left a waitress to deal with us. As a "manager" maybe she should have made a bit more effort, but frankly, after everything else I witnessed last night, I'm not really that surprised.

9) The final insult came when my girlfriend visited the toilet before leaving, which was dirty, and had no toliet paper!

So. Thanks for a FANTASTIC evening. It was definately a "A Night to Remember". 

I will NEVER step foot in one of your establishments ever again, and I will make it a point to make sure that anyone I know does'nt have to go through the same experience.

Your service is appalling. Your food is disgusting. Your standards are so low I'm not even sure you have any. Unimpressed.

Posted via email from Mark Kennard's Posterous