Sizzle and MooTools by the Numbers
There’s been a bit of hullabaloo this morning around a comment John Resig made on David Walsh’s blog where he asserts that Sizzle is 2x faster than MooTools. I posted this morning about how this is somewhat of an unfair comparison - that Sizzle is a selector engine and MooTools is a framework (that has its own selector engine).
The real question here is not which selector engine is faster (though that’s pertinent) but which framework is faster, by how much, and if it’s enough to matter. Valerio’s thoughtful post about why MooTools wouldn’t use Sizzle outlined several reasons why it doesn’t make sense, but the biggest reason is that the MooTools selector engine is actually faster than Sizzle when used as the MooTools selector engine. In other words, if MooTools dropped in Sizzle (which is larger in file size) for its current selector engine, our users would not see a benefit. This is really the main issue - he wasn’t arguing that MooTools is faster or as fast as jQuery - only that including Sizzle didn’t make sense.
So David posts instructions on how to use Sizzle, if you really want to, in MooTools. He asserts that doing so would provide no performance benefits and no functional benefits, which is really just an echo of Valerio’s argument. Again, he’s not comparing MooTools to jQuery - just that Sizzle + MooTools doesn’t really get you anything, and I agree.
So in an effort to understand that question, just that question - is MooTools w/ Sizzle faster or slower than MooTools without it, I put together a new SlickSpeed test. This test is the default SlickSpeed (Valerio made a version originally to test Sizzle that only ran the selector once because Sizzle used to cache but it doesn’t any more - this version of SlickSpeed runs each test numerous times).
In an effort to answer the main question, I’ve included in this test Sizzle, the MooTools selector engine on its own, and MooTools, and MooTools using Sizzle (as David outlined in his post). For good measure, I also include jQuery, though as it doesn’t post-process elements, you’ll see that it’s basically on par with Sizzle, as it should be.
I’ve made a google spreadsheet that others can add their results to. I’ll update this graphic below as more data comes in. I don’t have FF2 or IE8 handy, so if someone would be so kind as to fill that in…
As you can see, MooTools + Sizzle isn’t faster than MooTools on its own. This is the main question I wanted to answer.
This means that the selector engine in MooTools is the right choice for MooTools. If you look at the graphs though, you’ll see that in IE, MooTools really suffers. This is because IE doesn’t allow you to alter Element.prototype. As a result every time MooTools uses its selector engine in IE there is an overhead as each returned element must be extended with the new methods at that time. This is a design decision and it’s something we’re working on. Expect to see this aspect of performance improve in MooTools 1.3.
But as far as selector engines are concerned, Sizzle isn’t appreciably faster than the MooTools engine, especially when it’s used as the engine in MooTools.
I’ll finish with a portion of one of the comments I made this morning:
Our frameworks actually don’t have any other benchmarking method that I know of - selectors are the only thing. There’s no benchmark for CPU usage during animation or whatever.
But now the improvements are small. The initial speed-ups were orders of magnitude. We’re now squabbling over milliseconds. In a real world app, we’re talking about something like, maybe 50ms or so difference in performance for all the selectors running on a page. That’s what I mean by it not being a priority. Yes, maybe we could make MooTools perform 50ms faster, or a 100ms, but at this point, it’s not where we feel the biggest return on our investment lies.
After looking at the results of this test I think it’s reasonable to focus on speeding up how MooTools extends Elements, but not the selector engine. Expect to see improvements on that front in MooTools 1.3.
Some Input From The jQuery Team
Before I posted the above info I sent it off to Rey Bango. Aside from wanting to get advice from someone with a mustache, I thought it would be good to speak to someone on the jQuery team, and he fit both bills. Rey’s input to this post (which I haven’t altered other than to add this footnote) was the following:
“Our frameworks actually don’t have any other benchmarking method that I know of - selectors are the only thing. There’s no benchmark for CPU usage during animation or whatever.”
We have tests for DOM manipulation and event delgation which [are included in this set of tests as documented here].
I played with these tests which John has said “Sizzle is optimized to work best on the selectors that developers actually use - and the test represents that.” My issue with the test is that it is very repetitive - it tests against the same type of selectors repeatedly (#page, #masthead, #mastheadhd - essentially all just $ calls in MooTools - you wouldn’t use $$ for them - and .mastheadbd, .first, and .on which are all basically the same kind of selector - on a single css class name). John has created a version of SlickSpeed that repeats the types of selectors his framework is optimized for. Of course it performs faster. His argument that “MooTools optimizes for the wrong selectors” is, I think, presumptive. It would instead be accurate to say that it is not optimized for jQuery development.
The other point Rey made to me was he agrees with me that “we’re reaching the threshold of what can be achieved”. There are other things that need to be optimized, but at this point we’re trying to squeeze blood from a stone in many respects. In MooTools, we need to work on our post-processing to speed that up in IE. There are other things like DOM creation and manipulation that we need to optimize.
Included in the tests that Rey sent over were tests for DOM injection and event delegation, but I’m not convinced that the suite is real apples to apples. For instance, MooTools delegation doesn’t exist in the framework (I have a beta version in my own scripts, but that’s not official). The tests compare delegation in jQuery to MooTools standard event management which isn’t optimized for that sort of thing.
I’m sure that as such a test suite matures and is contributed to by other frameworks such as ours that we’ll see optimization race forward as we have with selectors. As it is, I’m still pretty convinced that Sizzle isn’t any faster than MooTools’ selector engine.
Follow @clientcide on twitter to get notified of new posts.
To follow me personally on twitter, follow @anutron.

February 12th, 2009 at 3:50 pm
To clarify my position on selectors, I do think we’re reaching a point where improvements in traversal performance will yield negligible results but that doesn’t mean that we’ll stop looking at tweaking things. The increased demands of JS applications will continue to push the demands of the language and frameworks so, as Aaron said, we really need to look at the whole framework and see how all pieces can be optimized to improve application performance.
February 12th, 2009 at 4:43 pm
just a thought, but have you tried extending slickspeed to run the test on a bigger sample of pages so the test is really done on REAL stuff?
Either running on the default slickspeed page or the Yahoo homepage, will only get the results for these two pages. And this is specially a point for Sizzle as they are ultimately building a selector engine that benchmarks like crazy in the Yahoo homepage and nothing else.
And don’t forget to compare the results in either AMD and Intel architectures, and even on older Pentium 4 based processors, as that’s where the performance difference would be noticeable. My 2 years old 2.7ghz AMD X2 runs the whole “single pass” slickspeed in 176ms in FF3 for both engines. Read that? a puny 176ms!!! for what, let me see… 43 queries! when was the last time you ran 43 queries in a row? let me answer fast: NEVER, unless you are Doing it Wrong. IE6 even came faster at 122ms for Sizzle and 112 ms for Moo (posibly the result of having 20+ extensions enabled at this time, or that IE6 is running native in my WinXP as I never cared to do the upgrade to IE7 in this machine)
Given those numbers, is it really important? is there a need to squeeze yet another millisecond today, knowing that CPUs are getting faster every day? does even a relatively high 250ms mean anything when the page may take several seconds to load due to any other factor like file transfer, DOM parsing or even network lag on each request?
Should we really care for users on ancient computers using ancient browsers? aren’t they used to get everything slow in the net? if they can’t run a moderately complex flash game with ease, then they are already expecting everything to be slow. To day, the slowest CPU I consider in my tests is an Atom, if it runs decently on it, then consider it gold.
FWIW, I will run the updated suite on my secondary machine which is an AMD Duron @ 600mhz from around 2001 and post here the results. I already tried it on FF3 and the results will puzzle every contender for the biggest selector engine e-penis out there.
February 12th, 2009 at 9:23 pm
as promised, test results on AMD Duron @ 600mhz
(from top to bottom: Firefox 3, IE6, Opera 9.6)
what this means: The worst case scenario (IE6) for the pure selector engines leave Sizzle at a dismal advantage of 240ms over god-knows-how-many-iterations.
As for decent browsers, they ace the test and even on this underpowered machine everything runs quite fast.
Now, I’m not really good at demographics, but I’m pretty sure that people using both an 8 year old PC and IE6 are not the average target audience of a site heavily loaded on javascript.
February 12th, 2009 at 11:43 pm
@gonchiki:
Loved your comments.
February 13th, 2009 at 1:24 am
@gonchuki Isn’t that a catch-22, that IE6/8-yo computer users aren’t the target demographic for a JS-heavy site? If JavaScript were to perform better in those cases, then it would be that much more appropriate to use it with that demographic!
I definitely agree, however, that these tests would be much more scientific if all factors concerned (computer architecture, test page markup, and sample size) were as statistically significant as possible. Testing a single page with one machine (and only once? or few times) only tells you how to optimize for that one page for that one machine!
February 13th, 2009 at 2:24 am
@gonchuki
You’re forgetting that Javascript some day has to work on handheld devices etc.
Other than that, it’s always good to look at the numbers your code is producing, and optimising it. These days of “hardware is cheaper than developers” must be taken with moderation.
February 13th, 2009 at 4:05 am
@David:
in fact if you think about it, there are little specific cases in which those kind of users exist: locked corporations by an asinine IT department, poor people without much computer knowledge (which you won’t be selling a product to them anyways), and Your Mom®. Facebook even took the initiative, and knowing that their site won’t work at a good speed on a combo of IE6 + old computer they give you a nice notice to encourage upgrading.
@Tim:
it does work today, and Safari on the iPhone and Opera in the rest of handheld devices today can easily handle anything out there. I guess it would be nice to run the slickspeed test on those devices too. I don’t know however about Windows Mobile, but as the desktop IE counterpart shown, difference between pure engines is so low in the worst case that the users cannot notice the difference. (specially on handheld, where 3G network lag eats away most of the “waiting time”)
February 14th, 2009 at 1:11 am
@gonchuki Hey, I’m all about disenfranchising noobs and their archaic machinery. I’m just not sure it’s commercially smart to just say the hell with them. I have many less-than-savvy friends with clunky, 5-10 yo Dells that run some kind of MSIE. I agree that these folk are accustomed to dealing with shit for load times. But, these are also consumers with checkbooks, and I reckon they’d be more likely to return to your site if it actually performs better than the other slow sites they’ve been to! Of course, if you have the analytics to prove that that’s not your target, then by all means to hell with them!!
February 16th, 2009 at 10:39 pm
The fact that the tests run _many_ iterations is because normal DOM query usage isn’t enough to notice a difference in speed. I especially liked gonchuki’s comments here:
‘when was the last time you ran 43 queries in a row? let me answer fast: NEVER, unless you are Doing it Wrong.’