Using Sizzle with Prototype

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;
};

  

About Brian

I'm a UI Software Engineer from Atlanta, GA who now lives in Cambridge, MA. Since late 2009, I've been working as Principal UI Engineer for Brightcove and loving exploring a new part of the US. Previously, I was Sr. UI Engineer for AutoTrader.com. I've been in this business since 1997 and I'm lucky to love what I do.
This entry was posted in briancrescimanno.com and tagged , , , , , , . Bookmark the permalink.

3 Responses to Using Sizzle with Prototype

  1. 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/selector-test/

    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

  2. dan levine says:

    It Just Works. Well played sir, thank you.

  3. dan levine says:

    note: IN FF 3.6 Sizzle ends up slower than straight prototype.

blog comments powered by Disqus