Using Sizzle with Prototype

March 24th, 2009 by Brian Leave a reply »

Recently, John Resig of jQuery fame released the selector engine used in the new version of jQuery called Sizzle.  Sizzle is a new take on using CSS selectors within Javascript and aims to be far more efficient than the methods commonly used by most current Javascript libraries.

Currently, I'm working on a large project that is nearly ready to deploy.  Most of the Javascript for this project has been written using the Prototype framework; however, we wanted to leverage the speed of the new Sizzle selectors engine in our project.  Given that we are deep in the QA process of the project, large code changes are discouraged to ensure code stability.  What we needed was a quick way to integrate Sizzle with Prototype.

The following code can be added to your Prototype 1.6.x file and will ensure that prototype's main element collection methods (Element#select and $$) both use Sizzle.  I've written before on how slow $$ can be, so these are two quick wins for Prototype performance.

//Overwrite findChildElements to use Sizzle http://sizzlejs.com
Selector.findChildElements = function(element, expression){
    expression = expression.join(", ");
    var results = Sizzle(expression, element);
    if(results.length > 0){
        for(var i=0; i < results.length; i++){
            results[i] = Element.extend(results[i]);
        }
    }
    return results;
};
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • Technorati
  • Twitter
  • DZone
  • HackerNews
Advertisement
  • Hello Brian,

    Thank you for this :) I wrote an article about it on my blog (crediting you for the code of course):
    http://www.victorstanciu.ro/sizzle-prototype/

    and i also put together the SlickSpeed Selectors Test to see the difference between Prototype and Prototype on Sizzle:
    http://dev.victorstanciu.ro/experimente/selecto...

    Sizzle boosts performance indeed, finally lowering the gap between jQuery and Prototype when it comes to selector engine speed. jQuery still wins hands-down on Internet Explorer (6, 7, 8) though.

    Thank you,
    Victor Stanciu
blog comments powered by Disqus