Demystifying the jQuery selectors optimization
April 28th 2009
Lately, I’ve been experimenting a lot with jQuery selectors. I wanted to find the best way to select elements depending on the situations. I’ve found some information online, but most of them didn’t have number and even worst, they were wrong.
So I’ve built a little demo page where you can run the tests yourself, that way you’ll be able to confirm the numbers. Please note that you need a browser that supports script profiling, that can be Firefox with Firebug or Safari/WebKit (those are the one I use). The version of jQuery I used is 1.3. These test are representative of browsers that supports getElementsByClassname (Firefox, Webkit, Opera), the numbers might be off in browsers such as IE6/IE7, lets look at the future, not the past
So to get started, it’s not true that the more precise you are in your selectors, the better. Some people tends to think (I was one of them) that $(’div.element’) was a better selector than only $(’.element’) because you target more preciselly what you are looking for. It’s not true, not only does jQuery execute more calls, but it’s also longer to process. Now lets say you want to be even more precise, not only you know the classname, but you also know this is the first element you’re looking for, your selector would be $(’div.element:first’). It’s even worst than only specifying the type.
So unless you really have to be really precise with your selectors, you should use a simple classname selector instead of the other 2.
Now, let’s move to selecting items at a specific position, once again, the simpler the better. Here are the cases I tested : $(’.sample-elements:first’), $(’.sample-elements .last:last’), $(’.sample-elements div.last:last’), $(’.sample-elements div:first’), $(’.sample-elements div:eq(2)’).
And I kept the best for the end, ID selector. If you can, this really is the way to go: $(’#second’). Not only is it the faster way to select an element, but it’s also the one that executes the less calls.
So that’s it for selectors, but what if you need to use complicated selectors.
What I usually do when I know I’ll have to use the same complicated selector multiple times, I store it in a global variable with a custom namespace. for example, this selector $(’.sample-elements div:eq(2)’) would translate to $third_sample_element. I add a dollar sign before it so I know this is a jQuery object. By doing this, you’ll do the complicated calls once, your object will then be stored in that variable and no more calls will be needed.
This kind of optimization is not “needed” when you develop small apps, but once you go into more compliated stuff, those small changes can really make a difference. And once you know the tricks, it’s really simple to put them into practice
Experimented it yourself? Have your own trick? Share them!
I don’t see graphs
The graph images are broken. Please fix.
Also, my experience contradicts your advice in the first point. I’ve found $(’div.element’) to be much faster than $(’element’).
Thanks.
As above, I’d like to see working graphs and demo as my experience is quite the opposite.
thank you for work
Strangely I get an error 404 by clicking the very first link (“a little demo page where you can run the tests yourself”)..looks like there is still some “margin-for-errors” 😉
view , for special offer at my estore
I know people have no proper idea for the asdfghjkl;’ and try to solve it with many of their tricks and they usually now works very well. But here all the idea and and way of the solution you can in the bunch.
By moving to this website you can learn how you can delete browsing history of bing and how you can manage it. As this website is known to hub of knowledge as I have visited this site is really helpful.
61exzv I see in my blog trackers significant traffic coming from facebook. My blog is not connected with facebook, I don at have an account there, and I can at see, who posts the linksany ideas?.
This website certainly has all of the information and facts I needed about this subject and didn at know who to ask.
You made some nice points there. I looked on the internet for the subject and found most people will consent with your website.
that I have really enjoyed browsing your blog posts.
you can always count on search engine marketing if you want to promote products online.
wow, awesome blog post.Really thank you! Cool.
if all webmasters and bloggers made good content as you probably did, the internet shall be much more useful than ever before.
Thanks so much for the article post.Thanks Again. Keep writing.
Very good blog! Do you have any tips and hints for aspiring writers?
Really appreciate you sharing this blog post. Will read on
I truly appreciate this blog post.Much thanks again. Will read on
Really enjoyed this article post.Really looking forward to read more. Will read on
Shiva habitait dans etait si enthousiaste, conducteur irait ranger de
Muchos Gracias for your article post.Really looking forward to read more.
Im obliged for the blog.Much thanks again. Want more.
You, my pal, ROCK! I found just the information I already searched all over the place and simply could not find it. What a great web site.
well written article. I all be sure to bookmark it and come back to read more
Perfectly written content, Really enjoyed studying.
Its hard to find good help I am forever saying that its difficult to procure good help, but here is
It as not all on Vince. Folks about him ended up stealing his money. Also when you feel his professional career is more than, you are an idiot.
Very neat article.Much thanks again. Really Great.
Thanks-a-mundo for the blog.Really thank you! Great.
if the buffalo in my head could speak german i would not know a god damm thing. What i do know is that the language of art is out of this world.
Wow! This can be one particular of the most useful blogs We ave ever arrive across on this subject. Actually Wonderful. I am also a specialist in this topic therefore I can understand your hard work.
I value the article.Really thank you! Will read on
Say, you got a nice blog.Much thanks again. Awesome.
I think this is a real great post.Much thanks again. Much obliged.
Your style is very unique compared to other folks I ave read stuff from. Thank you for posting when you have the opportunity, Guess I will just bookmark this site.
Very good post.Really looking forward to read more.
You will find some tips on how to write scientific paper https://edit-proofread.com/blog/scientific-paper-format This kind of stuff could really save ou in the future
the photo
Im grateful for the blog post.Really thank you! Awesome.
I would love to read your blog posts. This is really what I was looking for and I am very happy to come here!
I would love to read your blog posts. This is really what I was looking for and I am very happy to come here!
I really liked your post.Really looking forward to read more. Much obliged.
wow, awesome article post. Much obliged.