> The only thing I disagree with is the importance of winning the size war.

Up to this point all the emphasis has been on code size. At some point we
may want to look at speed, even if it makes the code bigger. One example:
When you say $(".myclass") the following things happen:

 * jQuery.find recursively collects every node in the document
    tree and copies that list into a second array;
 * jQuery.filter builds/compiles a regexp to get the class name,
    then builds/compiles a function to check the class name;
 * jQuery.grep calls that function once for each element;
 * That function calls jQuery.class.has, which builds/compiles
    a regexp to see if the class matches.

The last step is in an inner loop, so it really hurts. For a document with
1000 elements, the (nested) call to jQuery.class.has compiles the same
regexp 1000 times! Special-case handlers can hoist that out of the loop and
simplify the code paths significantly, but it adds more code. Plus you have
to decide how many special cases you want to optimize. Are lone classes and
ids enough? If so, something like this may do it.

var m = selector.match(/^(\w*|\*?)\.(\w+)$/;
if ( m ) {
  var els = context.getElementsByTagName(m[1]||"*");
  var pat = new RegExp("(^|\\s)" + m[2] + "(\\s|$)"); 
  for ( var i=0; i < els.length; i++ )
     if ( pat.test(els[i].className)
       ret.push(els[i]);
  return ret;
}
var m = selector.match(/^#(\w+)$/);
if ( m && context == document ) {
  var el = document.getElementById(m[1]);
  return el? [ el ] : [];
}


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to