This project is archived and is in readonly mode.

#947 ✓duplicate
mattcoz

Faster complex selector matching

Reported by mattcoz | July 18th, 2010 @ 06:40 PM | in 1.4.0 (closed)

Currently in Slick you're doing a brute force search of the document to find if the element matches the selector, this can be very slow. By leveraging the Slick parser's reverse function, we can speed this up quite a bit.

local.matchNode = function(node, selector){
    var parsed = ((selector.Slick) ? selector : this.Slick.parse(selector));
    if (!parsed) return true;
    
    var expressions = parsed.expressions, reversedExpressions;
        for (i = 0; (currentExpression = expressions[i]); i++){
            if(currentExpression.length == 1){
                var exp = currentExpression[0];
                if(this.matchSelector(node, (this.isXMLDocument) ? exp.tag : exp.tag.toUpperCase(), exp.id, exp.parts)) return true;
            }
        }
        for (i = 0; (currentExpression = expressions[i]); i++){
            if(currentExpression.length > 1){
                reversedExpressions = reversedExpressions || parsed.reverse().expressions;
                currentExpression = reversedExpressions[i];
                var exp = currentExpression.shift();
                if(this.matchSelector(node, (this.isXMLDocument) ? exp.tag : exp.tag.toUpperCase(), exp.id, exp.parts)){
                    if(!!this.search(node, { Slick: true, expressions: [ currentExpression ] }, null, true)) return true;
                }
            }
        }
        return false;
};

It's actually not quite as fast as some previous code I submitted(#574), but that no longer works with Slick.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Attachments

Tags

Pages