1 April 2009

CSS Selector Engines

CSS selectors are a great way to pick DOM nodes off a page and many JavaScript libraries use them extensively.

For those that have used JSquared, you will know that there is no CSS selector engine built in. Some people have asked why and I have always said that I think its simpler, faster, easier and makes a better performing application to use standard DOM methods to pick elements.

Well, yesterday I saw TaskSpeed and it got me thinking. I was about to start making a JSquared version of the tests in order to see what the performance was when I cam across this post which proved the point I wanted to make without me having to do any work!

Andrea shows that using native DOM methods is by far and away the fastest way to complete the tests. Whilst this isn't totally related to a CSS selector engine, it does show that native methods are the best and that certainly holds true for the few parts of the test where a selector engine would be used it it existed.

Of course, once all browsers in use support the Selectors API, all will be much improved,

1 comment:

Adam Silver said...

Good post. This is a great reason to use J2 as opposed to JQuery or Mootools.

If the library doesn't give the opportunity for cow boy developers to abuse $(selector) etc, then it will

a) force the developer to learn DOM scripting properly

b) make the JavaScript as performant as possible.