]> source.dussan.org Git - jquery.git/commitdiff
Optimize cleanData: allow direct access to data_priv for cleanup
authorRick Waldron <waldron.rick@gmail.com>
Wed, 17 Apr 2013 16:53:41 +0000 (12:53 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Mon, 13 May 2013 19:00:57 +0000 (15:00 -0400)
src/data.js
src/manipulation.js

index 95d06a00094ed3b036fbe998669fc9afbabd24c0..86dd4273fde5d82b0d204843dc0aa6ee0bfc30f8 100644 (file)
@@ -185,7 +185,9 @@ Data.prototype = {
                );
        },
        discard: function( owner ) {
-               delete this.cache[ this.key( owner ) ];
+               if ( owner[ this.expando ] ) {
+                       delete this.cache[ owner[ this.expando ] ];
+               }
        }
 };
 
index 58a598144b1a6370dc58d55fa3b5609b2c37d20f..ccf606d29e9c44eb0c07b48dda373e7dd2307c66 100644 (file)
@@ -433,35 +433,35 @@ jQuery.extend({
        },
 
        cleanData: function( elems ) {
-               var data, elem, type,
-                       l = elems.length,
-                       i = 0,
-                       special = jQuery.event.special;
-
-               for ( ; i < l; i++ ) {
-                       elem = elems[ i ];
-
-                       if ( jQuery.acceptData( elem ) ) {
+               var data, elem, events, type, key, j,
+                       special = jQuery.event.special,
+                       i = 0;
 
-                               data = data_priv.access( elem );
+               for ( ; (elem = elems[ i ]) !== undefined; i++ ) {
+                       if ( Data.accepts( elem ) ) {
+                               key = elem[ data_priv.expando ];
 
-                               if ( data ) {
-                                       for ( type in data.events ) {
-                                               if ( special[ type ] ) {
-                                                       jQuery.event.remove( elem, type );
+                               if ( key && (data = data_priv.cache[ key ]) ) {
+                                       events = Object.keys( data.events || {} );
+                                       if ( events.length ) {
+                                               for ( j = 0; (type = events[j]) !== undefined; j++ ) {
+                                                       if ( special[ type ] ) {
+                                                               jQuery.event.remove( elem, type );
 
-                                               // This is a shortcut to avoid jQuery.event.remove's overhead
-                                               } else {
-                                                       jQuery.removeEvent( elem, type, data.handle );
+                                                       // This is a shortcut to avoid jQuery.event.remove's overhead
+                                                       } else {
+                                                               jQuery.removeEvent( elem, type, data.handle );
+                                                       }
                                                }
                                        }
+                                       if ( data_priv.cache[ key ] ) {
+                                               // Discard any remaining `private` data
+                                               delete data_priv.cache[ key ];
+                                       }
                                }
                        }
-                       // Discard any remaining `private` and `user` data
-                       // One day we'll replace the dual arrays with a WeakMap and this won't be an issue.
-                       // (Splices the data objects out of the internal cache arrays)
-                       data_user.discard( elem );
-                       data_priv.discard( elem );
+                       // Discard any remaining `user` data
+                       delete data_user.cache[ elem[ data_user.expando ] ];
                }
        },