Refactor jQuery.data a bit to reduce property lookups
authorYehuda Katz <wycats@gmail.com>
Thu, 16 Jul 2009 07:32:31 +0000 (07:32 +0000)
committerYehuda Katz <wycats@gmail.com>
Thu, 16 Jul 2009 07:32:31 +0000 (07:32 +0000)
  - Also added jQuery.isEmptyObject

src/core.js
src/data.js

index b51886f..74b9fee 100644 (file)
@@ -291,6 +291,12 @@ jQuery.extend({
                return this.constructor.call(obj) === Object;
        },
 
+       isEmptyObject: function( obj ) {
+               var name = "";
+               for(name in obj) break;
+               return !name;
+       },
+
        // check if an element is in a (or is an) XML document
        isXMLDoc: function( elem ) {
                return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
index 1d5fe89..9cc76c6 100644 (file)
@@ -8,27 +8,24 @@ jQuery.extend({
                        windowData :\r
                        elem;\r
 \r
-               var id = elem[ expando ];\r
+               var id = elem[ expando ], cache = jQuery.cache;\r
 \r
                // Compute a unique ID for the element\r
-               if ( !id )\r
-                       id = elem[ expando ] = ++uuid;\r
+               if(!id) id = elem[ expando ] = ++uuid;\r
 \r
                // Only generate the data cache if we're\r
                // trying to access or manipulate it\r
-               if ( name && !jQuery.cache[ id ] )\r
-                       jQuery.cache[ id ] = {};\r
+               if ( name && !cache[ id ] )\r
+                       cache[ id ] = {};\r
 \r
-               // Prevent overriding the named cache with undefined values\r
-               if ( data !== undefined )\r
-                       jQuery.cache[ id ][ name ] = data;\r
+               var thisCache = cache[ id ];\r
 \r
-               if(name === true) return jQuery.cache[ id ]\r
+               // Prevent overriding the named cache with undefined values\r
+               if ( data !== undefined ) thisCache[ name ] = data;\r
 \r
-               // Return the named cache data, or the ID for the element\r
-               return name ?\r
-                       jQuery.cache[ id ][ name ] :\r
-                       id;\r
+               if(name === true) return thisCache\r
+               else if(name) return thisCache[name]\r
+               else return id\r
        },\r
 \r
        removeData: function( elem, name ) {\r
@@ -36,21 +33,16 @@ jQuery.extend({
                        windowData :\r
                        elem;\r
 \r
-               var id = elem[ expando ];\r
+               var id = elem[ expando ], cache = jQuery.cache, thisCache = cache[ id ];\r
 \r
                // If we want to remove a specific section of the element's data\r
                if ( name ) {\r
-                       if ( jQuery.cache[ id ] ) {\r
+                       if ( thisCache ) {\r
                                // Remove the section of cache data\r
-                               delete jQuery.cache[ id ][ name ];\r
+                               delete thisCache[ name ];\r
 \r
                                // If we've removed all the data, remove the element's cache\r
-                               name = "";\r
-\r
-                               for ( name in jQuery.cache[ id ] )\r
-                                       break;\r
-\r
-                               if ( !name )\r
+                               if( jQuery.isEmptyObject(thisCache) )\r
                                        jQuery.removeData( elem );\r
                        }\r
 \r
@@ -67,7 +59,7 @@ jQuery.extend({
                        }\r
 \r
                        // Completely remove the data cache\r
-                       delete jQuery.cache[ id ];\r
+                       delete cache[ id ];\r
                }\r
        },\r
        queue: function( elem, type, data ) {\r