Skip to content

Commit

Permalink
Overhauled the .remove() and .empty() methods to be much more efficie…
Browse files Browse the repository at this point in the history
…nt. Fixes bug #4222.
  • Loading branch information
jeresig committed Feb 23, 2009
1 parent 48164ee commit cb3f842
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/core.js
Expand Up @@ -1225,30 +1225,43 @@ jQuery.each({

remove: function( selector ) {
if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
// Prevent memory leaks
jQuery( "*", this ).add([this]).each(function(){
jQuery.event.remove(this);
jQuery.removeData(this);
});
if (this.parentNode)
if ( this.nodeType === 1 ) {
cleanData( this.getElementsByTagName("*") );
cleanData( [this] );
}

if ( this.parentNode ) {
this.parentNode.removeChild( this );
}
}
},

empty: function() {
// Remove element nodes and prevent memory leaks
jQuery(this).children().remove();
if ( this.nodeType === 1 ) {
cleanData( this.getElementsByTagName("*") );
}

// Remove any remaining nodes
while ( this.firstChild )
while ( this.firstChild ) {
this.removeChild( this.firstChild );
}
}
}, function(name, fn){
jQuery.fn[ name ] = function(){
return this.each( fn, arguments );
};
});

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
function num(elem, prop) {
return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0;
Expand Down

0 comments on commit cb3f842

Please sign in to comment.