Overhauled the .remove() and .empty() methods to be much more efficient. Fixes bug #4222.

This commit is contained in:
John Resig 2009-02-23 16:32:45 +00:00
parent 48164ee603
commit cb3f842c88

View file

@ -1225,23 +1225,27 @@ jQuery.each({
remove: function( selector ) { remove: function( selector ) {
if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) { if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
// Prevent memory leaks if ( this.nodeType === 1 ) {
jQuery( "*", this ).add([this]).each(function(){ cleanData( this.getElementsByTagName("*") );
jQuery.event.remove(this); cleanData( [this] );
jQuery.removeData(this); }
});
if (this.parentNode) if ( this.parentNode ) {
this.parentNode.removeChild( this ); this.parentNode.removeChild( this );
}
} }
}, },
empty: function() { empty: function() {
// Remove element nodes and prevent memory leaks // Remove element nodes and prevent memory leaks
jQuery(this).children().remove(); if ( this.nodeType === 1 ) {
cleanData( this.getElementsByTagName("*") );
}
// Remove any remaining nodes // Remove any remaining nodes
while ( this.firstChild ) while ( this.firstChild ) {
this.removeChild( this.firstChild ); this.removeChild( this.firstChild );
}
} }
}, function(name, fn){ }, function(name, fn){
jQuery.fn[ name ] = function(){ jQuery.fn[ name ] = function(){
@ -1249,6 +1253,15 @@ jQuery.each({
}; };
}); });
function cleanData( elems ) {
for ( var i = 0, l = elems.length; i < l; i++ ) {
var id = elems[i][expando];
if ( id ) {
delete jQuery.cache[ id ];
}
}
}
// Helper function used by the dimensions and offset modules // Helper function used by the dimensions and offset modules
function num(elem, prop) { function num(elem, prop) {
return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0; return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0;