Merged my speed improvements into the jQuery core (is actually 200b smaller now!). Additionally, added in some better reporting to the test suite, and fixed a bug with how selector tests were performmed.

This commit is contained in:
John Resig 2007-03-24 21:55:03 +00:00
parent 1b9f4d3d2e
commit ec2b688920
3 changed files with 45 additions and 58 deletions

View file

@ -172,17 +172,33 @@ function ok(a, msg) {
function isSet(a, b, msg) { function isSet(a, b, msg) {
var ret = true; var ret = true;
if ( a && b && a.length == b.length ) { if ( a && b && a.length == b.length ) {
for ( var i in a ) for ( var i = 0; i < a.length; i++ )
if ( a[i] != b[i] ) if ( a[i] != b[i] )
ret = false; ret = false;
} else } else
ret = false; ret = false;
if ( !ret ) if ( !ret )
_config.Test.push( [ ret, msg + " expected: " + b + " result: " + a ] ); _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
else else
_config.Test.push( [ ret, msg ] ); _config.Test.push( [ ret, msg ] );
} }
function serialArray( a ) {
var r = [];
for ( var i = 0; i < a.length; i++ ) {
var str = a[i].nodeName;
if ( str ) {
str = str.toLowerCase();
if ( a[i].id )
str += "#" + a[i].id;
} else
str = a[i];
r.push( str );
}
return "[ " + r.join(", ") + " ]"
}
/** /**
* Returns an array of elements with the given IDs, eg. * Returns an array of elements with the given IDs, eg.
* @example q("main", "foo", "bar") * @example q("main", "foo", "bar")
@ -201,7 +217,7 @@ function q() {
* @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar' * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
*/ */
function t(a,b,c) { function t(a,b,c) {
var f = jQuery.find(b); var f = jQuery(b);
var s = ""; var s = "";
for ( var i = 0; i < f.length; i++ ) for ( var i = 0; i < f.length; i++ )
s += (s && ",") + '"' + f[i].id + '"'; s += (s && ",") + '"' + f[i].id + '"';

39
src/jquery/jquery.js vendored
View file

@ -817,9 +817,9 @@ jQuery.fn = jQuery.prototype = {
* @cat DOM/Traversing * @cat DOM/Traversing
*/ */
find: function(t) { find: function(t) {
return this.pushStack( jQuery.map( this, function(a){ return this.pushStack( jQuery.unique( jQuery.map( this, function(a){
return jQuery.find(t,a); return jQuery.find(t,a);
}), t ); }) ), t );
}, },
/** /**
@ -1629,19 +1629,26 @@ jQuery.extend({
* @cat JavaScript * @cat JavaScript
*/ */
merge: function(first, second) { merge: function(first, second) {
var r = [].slice.call( first, 0 );
// Now check for duplicates between the two arrays
// and only add the unique items
for ( var i = 0, sl = second.length; i < sl; i++ ) for ( var i = 0, sl = second.length; i < sl; i++ )
// Check for duplicates first.push(second[i]);
if ( jQuery.inArray( second[i], r ) == -1 )
// The item is unique, add it
first.push( second[i] );
return first; return first;
}, },
unique: function(first) {
var r = [], num = jQuery.mergeNum++;
for ( var i = 0, fl = first.length; i < fl; i++ )
if ( first[i].mergeNum != num ) {
first[i].mergeNum = num;
r.push(first[i]);
}
return r;
},
mergeNum: 0,
/** /**
* Filter items out of an array, by using a filter function. * Filter items out of an array, by using a filter function.
* *
@ -1735,17 +1742,7 @@ jQuery.extend({
} }
} }
var r = result.length ? [ result[0] ] : []; return result;
check: for ( var i = 1, rl = result.length; i < rl; i++ ) {
for ( var j = 0; j < i; j++ )
if ( result[i] == r[j] )
continue check;
r.push( result[i] );
}
return r;
} }
}); });

View file

@ -195,7 +195,7 @@ jQuery.extend({
if ( ret[0] == context ) ret.shift(); if ( ret[0] == context ) ret.shift();
// Merge the result sets // Merge the result sets
jQuery.merge( done, ret ); done = jQuery.merge( done, ret );
// Reset the context // Reset the context
r = ret = [context]; r = ret = [context];
@ -243,29 +243,25 @@ jQuery.extend({
// We need to find all descendant elements, it is more // We need to find all descendant elements, it is more
// efficient to use getAll() when we are already further down // efficient to use getAll() when we are already further down
// the tree - we try to recognize that here // the tree - we try to recognize that here
jQuery.each( ret, function(){ for ( var i = 0, rl = ret.length; i < rl; i++ ) {
// Grab the tag name being searched for // Grab the tag name being searched for
var tag = m[1] != "" || m[0] == "" ? "*" : m[2]; var tag = m[1] != "" || m[0] == "" ? "*" : m[2];
// Handle IE7 being really dumb about <object>s // Handle IE7 being really dumb about <object>s
if ( jQuery.nodeName(this, "object") && tag == "*" ) if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" )
tag = "param"; tag = "param";
jQuery.merge( r, r = jQuery.merge( r, ret[i].getElementsByTagName( tag ));
m[1] != "" && ret.length != 1 ? }
jQuery.getAll( this, [], m[1], m[2], rec ) :
this.getElementsByTagName( tag )
);
});
// It's faster to filter by class and be done with it // It's faster to filter by class and be done with it
if ( m[1] == "." && ret.length == 1 ) if ( m[1] == "." )
r = jQuery.grep( r, function(e) { r = jQuery.grep( r, function(e) {
return rec.test(e.className); return rec.test(e.className);
}); });
// Same with ID filtering // Same with ID filtering
if ( m[1] == "#" && ret.length == 1 ) { if ( m[1] == "#" ) {
// Remember, then wipe out, the result set // Remember, then wipe out, the result set
var tmp = r; var tmp = r;
r = []; r = [];
@ -300,7 +296,7 @@ jQuery.extend({
if ( ret && ret[0] == context ) ret.shift(); if ( ret && ret[0] == context ) ret.shift();
// And combine the results // And combine the results
jQuery.merge( done, ret ); done = jQuery.merge( done, ret );
return done; return done;
}, },
@ -363,28 +359,6 @@ jQuery.extend({
// and the modified expression string (t) // and the modified expression string (t)
return { r: r, t: t }; return { r: r, t: t };
}, },
getAll: function( o, r, token, name, re ) {
for ( var s = o.firstChild; s; s = s.nextSibling )
if ( s.nodeType == 1 ) {
var add = true;
if ( token == "." )
add = s.className && re.test(s.className);
else if ( token == "#" )
add = s.getAttribute("id") == name;
if ( add )
r.push( s );
if ( token == "#" && r.length ) break;
if ( s.firstChild )
jQuery.getAll( s, r, token, name, re );
}
return r;
},
/** /**
* All ancestors of a given element. * All ancestors of a given element.